Longest Consecutive Sequence
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Write longestConsecutive(nums): return the length of the longest run of whole numbers that follow on from each other, like 1, 2, 3, 4, that are all in nums. nums isn't sorted, may hold negative numbers and repeats (a repeat counts once), and you may reorder it. An empty list gives 0. For example, longestConsecutive([100, 4, 200, 1, 3, 2]) is 4. The checks include 100,000 numbers, so searching the list again for each next number is too slow.
Your code must pass
- Finds the longest run of consecutive numbers
- Counts repeated numbers once
- Handles negative numbers
- Returns 0 for an empty list
- Is fast enough for 100,000 numbers