We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0e9b086 commit e8548e0Copy full SHA for e8548e0
JooKangSan/[week6]Set/Longest_Consecutive_Sequence.js
@@ -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