medium

Longest Consecutive Sequence

Return the length of the longest run of consecutive integer values in an unsorted array. Duplicates do not extend a run. Aim for expected linear time.

Constraints
  • 0 ≤ n ≤ 100000
  • -10^9 ≤ nums[i] ≤ 10^9
Examples
in: [[8,2,4,3,20]]
out: 3
Unsorted run
in: [[]]
out: 0
Empty

Code it yourself

Solve in
Practice journal →Draft saved in this browser.
Public test cases · contract v1

Arguments are passed to your function. Tests are public practice checks, not hidden interview grading. Passing does not prove every possible input.

  • Unsorted run
    [[8,2,4,3,20]] → 3
  • Empty
    [[]] → 0
  • Duplicates do not extend run
    [[1,2,2,3]] → 3
  • Across zero
    [[-2,-1,0,1,8]] → 4
  • One value
    [[5]] → 1
Hints:
Which approach applies?

Choose an approach to check your pattern recognition, or reveal the discussion when you need help.