Skip to content

Commit 8a17b65

Browse files
authored
feat: longest-consecutive-sequence
1 parent f29764f commit 8a17b65

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var longestConsecutive = function(nums) {
6+
const numsSet = new Set(nums);
7+
8+
9+
if(nums.length === 0) return 0
10+
11+
const sortedNums = [...numsSet].sort((a,b) => a-b);
12+
13+
const results = [];
14+
15+
let result = 1;
16+
17+
for(let i = 0; i < sortedNums.length-1; i++){
18+
const current = sortedNums[i];
19+
const next = sortedNums[i+1];
20+
21+
if((current + 1) === next){
22+
result += 1;
23+
}else{
24+
results.push(result);
25+
result = 1;
26+
}
27+
}
28+
29+
results.push(result);
30+
31+
32+
return Math.max(...results)
33+
};

0 commit comments

Comments
 (0)