Longest Consecutive Sequence
Problem statement
Given an unsorted integer array nums, return the length of the longest sequence of consecutive integer values.
The values of a sequence do not need to appear consecutively in the input. Design an algorithm with expected O(n) time.
Function
longestConsecutive(nums: int[]) → intExamples
Example 1
nums = [100,4,200,1,3,2]return = 4The longest sequence is 1,2,3,4.
Example 2
nums = [0,3,7,2,5,8,4,6,0,1]return = 9The values from 0 through 8 are all present.
Constraints
0 <= nums.length <= 100000.- Each value is in
[-10^9, 10^9].
Source note: Author-written prompt excerpt retained for source fidelity.