Skip to content

Commit 8f795d6

Browse files
committed
feat: longest-consecutive-sequence
1 parent d666395 commit 8f795d6

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)