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 f29764f commit 8a17b65Copy full SHA for 8a17b65
longest-consecutive-sequence/hozzijeong.js
@@ -0,0 +1,33 @@
1
+/**
2
+ * @param {number[]} nums
3
+ * @return {number}
4
+ */
5
+var longestConsecutive = function(nums) {
6
+ const numsSet = new Set(nums);
7
+
8
9
+ if(nums.length === 0) return 0
10
11
+ const sortedNums = [...numsSet].sort((a,b) => a-b);
12
13
+ const results = [];
14
15
+ let result = 1;
16
17
+ for(let i = 0; i < sortedNums.length-1; i++){
18
+ const current = sortedNums[i];
19
+ const next = sortedNums[i+1];
20
21
+ if((current + 1) === next){
22
+ result += 1;
23
+ }else{
24
+ results.push(result);
25
+ result = 1;
26
+ }
27
28
29
30
31
32
+ return Math.max(...results)
33
+};
0 commit comments