Find the longest consecutive subsequence number in an Array?
Code
import Foundation
func longestConsecutiveSubsequence(arr: [Int]) -> Int {
var numSet = Set(arr)
var longestStreak = 0
for num in arr {
print(numSet)
if !numSet.contains(num - 1) {
print(num)
var currentNum = num
var currentStreak = 1
while numSet.contains(currentNum + 1) {
currentNum += 1
currentStreak += 1
}
longestStreak = max(longestStreak, currentStreak)
}
}
return longestStreak
}