Skip to content

Commit ee24006

Browse files
author
Heeseung Koo
committed
w01: 128. Longest Consecutive Sequence
1 parent 9815473 commit ee24006

File tree

1 file changed

+13
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)