Skip to content

Commit f015773

Browse files
committed
longest-consecutive-sequence
1 parent 2cf182b commit f015773

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function longestConsecutive(nums: number[]): number {
2+
const set = new Set(nums);
3+
let maxCount = 0;
4+
5+
for (const num of set) {
6+
const isStart = !set.has(num - 1);
7+
if(!isStart) continue;
8+
9+
let count = 0;
10+
let _num = num;
11+
while(true) {
12+
count += 1;
13+
const hasNext = set.has(_num + 1);
14+
15+
if(!hasNext) {
16+
if(count > maxCount) maxCount = count;
17+
18+
break;
19+
}
20+
21+
set.delete(_num);
22+
_num+=1;
23+
}
24+
}
25+
26+
return maxCount
27+
};

0 commit comments

Comments
 (0)