Skip to content

Commit bb8d394

Browse files
committed
longest consecutive sequence solution
1 parent fa9ea36 commit bb8d394

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package youngDaLee
2+
3+
func longestConsecutive(nums []int) int {
4+
numSet := make(map[int]bool)
5+
for _, num := range nums {
6+
numSet[num] = true
7+
}
8+
9+
longestStreak := 0
10+
11+
for num := range numSet {
12+
if !numSet[num-1] {
13+
currentNum := num
14+
currentStreak := 1
15+
16+
for numSet[currentNum+1] {
17+
currentNum++
18+
currentStreak++
19+
}
20+
21+
if currentStreak > longestStreak {
22+
longestStreak = currentStreak
23+
}
24+
}
25+
}
26+
27+
return longestStreak
28+
}

0 commit comments

Comments
 (0)