Skip to content

Commit 7c68cbd

Browse files
committed
longest-consecutive-sequence solution
1 parent 28aca7d commit 7c68cbd

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 longest = 0;
10+
11+
for (const num of numSet) {
12+
if(!numSet.has(num-1)) {
13+
let currentNum = num;
14+
let currentLength = 1;
15+
16+
while(numSet.has(currentNum+1)) {
17+
currentNum++;
18+
currentLength++;
19+
}
20+
21+
longest = Math.max(longest, currentLength)
22+
}
23+
}
24+
return longest;
25+
};

0 commit comments

Comments
 (0)