Skip to content
AI360Xpert
Beta
Difficulty: MediumArrays & Hashing

Longest Consecutive Sequence

Problem in Plain English

Given an unsorted array of integers, return the length of the longest run of consecutive integers it contains. The order in the array does not matter and the algorithm must run in linear time.

Problem Statement

Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.

You must write an algorithm that runs in O(n)O(n) time. Sorting would place consecutive values side by side, a hash set instead supports expected constant-time membership checks without sorting.

Constraints

  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9

Examples

Example 1

Input
nums = [100, 4, 200, 1, 3, 2]
Output
4

The longest consecutive run is [1, 2, 3, 4], which has length 4.

Example 2

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

The longest consecutive run is [0, 1, 2, 3, 4, 5, 6, 7, 8], which has length 9.

Example 3

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

The distinct values 0, 1, and 2 form one run of length 3. The second occurrence of 1 adds no new consecutive value.

Example 4

Input
nums = []
Output
0

No integers means there is no run to count.

Intuition

Consecutive values differ by one; their positions in the array do not matter. In [100, 4, 200, 1, 3, 2], the values 1 through 4 form a run even though they are scattered. Store values in a set for quick membership checks. Start counting only where the previous integer is missing: 1 starts the run, while 2, 3, and 4 do not.

Approaches

Sorting

Solution Details

Reveal Sorting: intuition, complexity, and code

Hints

Hint 1
Sorting first turns the problem into a neighbor-counting scan, but check what that costs on 10^5 elements.
Hint 2
Linear time needs average O(1)O(1) membership tests, which points at storing every value in a hash set.
Hint 3
Only measure runs forward from values whose predecessor num - 1 is missing, so each run is counted exactly once.

Edge Cases

  • An empty array has no run at all and returns 0.
  • Duplicates such as [1, 2, 0, 1] must not inflate the run; the set collapses them and the sort skips equal neighbors.
  • Negative values work identically: [-2, 0, -1] returns 3.
  • Separated values such as [10, 30, 50] return 1; a single value also returns 1.
  • Values at -1000000000 and 1000000000 are allowed. Their neighbor checks still fit Java int and exact JavaScript integer arithmetic.

Common Mistakes and Interview Tips

  • Counting a forward run from every value instead of only run starts, which turns the hash set scan back into O(n2)O(n^2) on long runs.
  • Forgetting to skip equal neighbors in the sorting scan, so a duplicate wrongly resets the current run.
  • Looping over nums rather than distinct set values can recount a long run many times when its start is duplicated.
  • Interview tip: explain the total successor visits across all runs to justify the nested loops, and state the expected constant-time hashing assumption.
  • Claiming the sorting solution uses constant total space ignores the sorting implementation; also disclose that it changes the input.

Key Takeaway

For consecutiveness questions, store values in a hash set and anchor each piece of work at a start condition so no element is processed twice.