Longest Consecutive Sequence

Problem Given an unsorted integer array, return the length of the longest run of consecutive integers (by value, not by position).

Input / Output

  • Input: integer array nums.
  • Output: the length of the longest consecutive-value run.

Constraints

  • n up to 10^5; O(n) required — sorting (O(n log n)) is the explicit thing to beat.
  • Duplicates may appear and must not inflate a run's length.

Example

  • [100,4,200,1,3,2] -> 4 (the run 1,2,3,4).
  • [0,3,7,2,5,8,4,6,0,1] -> 9.
asked …
LeaderboardSalaryAccount