Skip to content

Commit f858c17

Browse files
authored
Create 128-longest-consecutive-sequence.js
1 parent 02890bc commit f858c17

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

128-longest-consecutive-sequence.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const longestConsecutive = function(nums) {
6+
if(nums.length === 0) return 0
7+
nums.sort((a, b) => a - b)
8+
let max = 1
9+
let cur = 1
10+
for(let i = 1; i < nums.length; i++) {
11+
if(nums[i] - nums[i-1] === 1) {
12+
cur += 1
13+
max = Math.max(max, cur)
14+
} else if(nums[i] - nums[i-1] === 0) {
15+
16+
} else {
17+
cur = 1
18+
}
19+
}
20+
21+
return max
22+
};

0 commit comments

Comments
 (0)