Longest Consecutive Sequence

Given an unsorted integer array nums, return the length of its longest sequence of consecutive integer values. The values in a sequence do not need to appear…

Given an unsorted integer array `nums`, return the length of its longest sequence of consecutive integer values. The values in a sequence do not need to appear next to each other in the input, and duplicate values do not extend a sequence.

- `0 <= nums.length <= 10^5` - `-10^9 <= nums[i] <= 10^9` - Design the optimal solution to run in expected `O(n)` time. - The interactive visualizer accepts at most 12 values between -10,000 and 10,000; other valid inputs are execution-only.

Example 1: input nums = [100,4,200,1,3,2]; output 4. The longest consecutive sequence is [1,2,3,4].

Example 2: input nums = [0,3,7,2,5,8,4,6,0,1]; output 9. The distinct values 0 through 8 form a sequence of length 9.

Category: Arrays & Hashing. Topic: arrays. Difficulty: Medium. Patterns: hashing, linear-scan.