Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence. You must write an algorithm that runs in O(n) time.
Example 1
Input: nums = [100,4,200,1,3,2]
Output: 4
Explanation: The numbers 1, 2, 3 and 4 are all present, forming a run of 4 consecutive values. 100 and 200 stand alone.
Example 2
Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9
Explanation: Every value from 0 to 8 is present (the duplicate 0 counts once), forming a run of 9 consecutive values.
Example 3
Input: nums = []
Output: 0
Explanation: The array is empty, so there is no sequence.
For each number `x`, we could count how long the run `x, x+1, x+2, …` goes. If we do that for *every* number, we re-walk the same sequence many times. The trick: only count a run when `x` is the **start of its sequence** — i.e. when `x - 1` is not in the set.
Run checks all cases above. Submit evaluates all test cases.