Skip to content

Commit 3720c48

Browse files
committed
Time: 39 ms (58.68%), Space: 80.8 MB (56.22%) - LeetHub
source:72ae2021a1d91491b4ce60e69abb9becefee4e5f
1 parent ba4423f commit 3720c48

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var longestConsecutive = function(nums) {
6+
if (nums.length === 0) return 0;
7+
8+
const numSet = new Set(nums);
9+
let maxLen = 0;
10+
11+
for (const num of numSet) {
12+
if (!numSet.has(num - 1)) {
13+
let currentNum = num;
14+
let currentLen = 1;
15+
16+
while (numSet.has(currentNum + 1)) {
17+
currentNum += 1;
18+
currentLen += 1;
19+
}
20+
maxLen = Math.max(maxLen, currentLen);
21+
}
22+
}
23+
24+
return maxLen;
25+
26+
};

0 commit comments

Comments
 (0)