Longest Consecutive Sequence

Arrays & Hashing, problem 8 of 9

Longest Consecutive Sequence

Medium

LC #128

hash setarray

Not attempted yet

Given an unsorted integer list nums, return the length of the longest run of consecutive integers (like 4, 5, 6, 7) whose values all appear in nums. Their positions don't matter.

Your solution must run in O(n) time.

Example 1

Input: nums = [100, 4, 200, 1, 3, 2]
Output: 4
The run is 1, 2, 3, 4.

Example 2

Input: nums = [0, 3, 7, 2, 5, 8, 4, 6, 0, 1]
Output: 9

Example 3

Input: nums = [1, 0, 1, 2]
Output: 3

Constraints

  • 0 ≤ len(nums) ≤ 2 · 10^5
  • -10^9 ≤ nums[i] ≤ 10^9

Python

Loading draft…

Test results

9 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 6 hidden

Run examples, then submit all tests.