Skip to content

Commit e8b0675

Browse files
committed
Add a solution for longest-consecutive-sequence
1 parent 0e27667 commit e8b0675

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function longestConsecutive(nums: number[]): number {
2+
const st = new Set(nums);
3+
4+
let mx = 0;
5+
for (const n of st) {
6+
if (st.has(n - 1)) continue;
7+
8+
let len = 1;
9+
while (st.has(n + len)) {
10+
len++;
11+
}
12+
13+
mx = Math.max(mx, len);
14+
}
15+
16+
return mx;
17+
}

0 commit comments

Comments
 (0)