Longest Consecutive Sequence
Given an unsorted integer array nums, return the length of the longest run of consecutive integers (values differing by 1) that appear in the array. The algorithm must run in O(n) time.
Open official problem prompt ↗Measure the longest span of back-to-back integers present in the data, regardless of their order in the array.
Guests wear numbered badges scattered around a room. To find the longest unbroken numeric line, you only bother forming a line starting from a guest whose number-minus-one is not in the room, then count how far the line stretches upward.
- Input
- nums = [100, 4, 200, 1, 3, 2]
- Output
- 4
- Why
- The consecutive run 1, 2, 3, 4 has length 4; no longer run exists.
0 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9