Skip to content

Commit 8b75c76

Browse files
committed
Add 128-Longest-consecutive-sequence.js
1 parent 51bcb14 commit 8b75c76

File tree

1 file changed

+24
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)