/*
* @lc app=leetcode.cn id=128 lang=javascript
*
* [128] 最长连续序列
*/
// @lc code=start
/**
* @param {number[]} nums
* @return {number}
*/
var longestConsecutive = function (nums) {
const hash = new Set(nums);
let count = 0;
let ans = 0;
for (let cur of hash) {
if (hash.has(cur - 1)) {
continue;
}
count++;
let temp = cur + 1;
while (hash.has(temp)) {
count++;
temp += 1;
}
ans = Math.max(count, ans);
count = 0;
}
return ans;
};
// @lc code=end
console.log(longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1]))
console.log(longestConsecutive([100, 4, 200, 1, 3, 2]))