Longest Consecutive Sequence
- Array
- Hash Set
Given an unsorted integer array nums, return the length of the longest sequence of consecutive values.
The values in a sequence do not need to be adjacent in the input array.
Sequence hidden among outliers
- Input
"nums": [41, 39, 42, 100, 40, 12]
- Output
4
Explanation: The values 39, 40, 41, and 42 form the longest run.
Sequence crossing zero
- Input
"nums": [-4, -1, -3, 8, -2, 0, 14]
- Output
5
Explanation: The consecutive values from -4 through 0 give a run of length 5.
Constraints
- 0 <= nums.length <= 100,000
- -1,000,000,000 <= nums[i] <= 1,000,000,000
Follow-up
Design an O(n) expected-time solution.