Skip to content

Commit e8548e0

Browse files
committed
Longest Consecutive Sequence / 중급
1 parent 0e9b086 commit e8548e0

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var longestConsecutive = function (nums) {
6+
const numSet = new Set(nums);
7+
let longestStreak = 0;
8+
for (const num of numSet) {
9+
if (!numSet.has(num - 1)) {
10+
let currentNum = num;
11+
let currentStreak = 1;
12+
while (numSet.has(currentNum + 1)) {
13+
currentNum += 1;
14+
currentStreak += 1;
15+
}
16+
longestStreak = Math.max(longestStreak, currentStreak);
17+
}
18+
}
19+
return longestStreak;
20+
};

0 commit comments

Comments
 (0)