Skip to content

Commit b86951e

Browse files
committed
longest-consecutive-sequence
1 parent e902aaa commit b86951e

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+
function longestConsecutive(nums: number[]): number {
2+
if (!nums.length) return 0;
3+
4+
nums.sort((a, b) => a - b);
5+
let longest = 1;
6+
let currentStreak = 1;
7+
8+
for (let i = 1; i < nums.length; i++) {
9+
if (nums[i] === nums[i - 1]) continue;
10+
if (nums[i] === nums[i - 1] + 1) {
11+
currentStreak++;
12+
} else {
13+
longest = Math.max(longest, currentStreak);
14+
currentStreak = 1;
15+
}
16+
}
17+
18+
return Math.max(longest, currentStreak);
19+
}
20+
21+
/**
22+
* * Runtime: 37ms / 74.15%
23+
* * Memory: 67.88MB / 93.54%
24+
*/

0 commit comments

Comments
 (0)