forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path128-Longest-Consecutive-Sequence.swift
More file actions
23 lines (23 loc) · 1008 Bytes
/
128-Longest-Consecutive-Sequence.swift
File metadata and controls
23 lines (23 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
func longestConsecutive(_ nums: [Int]) -> Int {
// remove duplicates numbers
let numSet = Set(nums)
var longestSequenceCount = 0
// iterate through each number in the numSet
// find the lowest number for a sequence, if num - 1 is not there in the numSet, it means num - 1 is the
// lowest for that particular sequence
for num in numSet where !numSet.contains(num - 1) {
// num is lowest number for the probable longest consecutive sequence
var visitingNum = num + 1
var currentSequenceCount = 1
// check if next number exists in the numSet, if so then increase the count
while numSet.contains(visitingNum) {
currentSequenceCount += 1
visitingNum += 1
}
// set the maximum count
longestSequenceCount = max(longestSequenceCount, currentSequenceCount)
}
return longestSequenceCount
}
}