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 d666395 commit 8f795d6Copy full SHA for 8f795d6
1 file changed
longest-consecutive-sequence/soobing3.ts
@@ -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