Skip to content

Commit e4c3840

Browse files
Jaehyeon Robert HanJaehyeon Robert Han
authored andcommitted
longest-consecutive-sequence solution
1 parent cf8bb29 commit e4c3840

File tree

1 file changed

+25
-0
lines changed
  • longest-consecutive-sequence

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+
let set = new Set();
8+
for(const n of nums) {
9+
set.add(n);
10+
}
11+
let max = 0;
12+
for(let n of set) {
13+
if(!set.has(n-1)) {
14+
let count = 0;
15+
while(set.has(n++)) {
16+
count++;
17+
}
18+
max = Math.max(max, count);
19+
}
20+
}
21+
return max;
22+
};
23+
24+
/* Test code */
25+
console.log(longestConsecutive([9,1,4,7,3,-1,0,5,8,-1,6])); // true

0 commit comments

Comments
 (0)