Browse curriculum

Longest Consecutive Sequence

Find the length of the longest run of consecutive integers in an unsorted array in O(n) with a hash set.

Problem Understanding

Longest Consecutive Sequence: given an unsorted array of integers nums, return the length of the longest run of consecutive values (x, x+1, x+2, …) that all appear in the array. The run can use the values in any order of the array. The target is O(n) time.

Example: [100, 4, 200, 1, 3, 2] returns 4, for 1, 2, 3, 4.

Attempt 1: Sort, Then Scan

Sort the array and walk it, extending the current run while each value is one more than the previous (skipping duplicates). It is simple and correct, but the sort costs O(n log n). A naive alternative — for every value, count upward with a linear search for each next value — is O(n³) or O(n²) with a set.

Visualizing the Issue

With a set, counting upward from every value repeats work. In 1, 2, 3, 4, counting from 1 walks four values, from 2 walks three, from 3 two, from 4 one: the same run is walked four times. On a long run of n values that is O(n²).

The Intuition: Count Each Run Once, From Its Start

A value starts a run exactly when its predecessor is missing:

  1. Put every number in a hash set.
  2. For each value num in the set, if num - 1 is in the set, skip it — its run will be counted from the start.
  3. Otherwise count upward: num + 1, num + 2, … while each is in the set, and keep the longest length.

Every run is walked once, from its start, and every other value is rejected with one lookup — O(n) in total.

Interactive Walkthrough

[100, 4, 200, 1, 3, 2]

BUILD THE SET

set, sorted for display

1

2

•

3

•

4

•

100

200

num

—

Length

—

Best

0

 

Put the 6 numbers in a set: 6 distinct values. best = 0

Next

99 is not in the set: 100 starts a run. length = 1

Array

The set's values are drawn in sorted order so runs show up as neighbours — the algorithm itself never sorts. Watch values with a predecessor get skipped, and each run fill in from its start; the longest run so far stays highlighted.

The approach, step by step

  1. Build a set

    Insert every number into a hash set; duplicates collapse.

  2. Start only at run starts

    For each value, skip it if value - 1 is in the set; otherwise it starts a run.

  3. Count the run

    From a start, count upward while the next value is in the set, and keep the longest length.

Frequently asked questions

How is Longest Consecutive Sequence solved in O(n) time?

Put every number in a hash set. A number starts a run only if num - 1 is not in the set; from each start, count upward while num + length is in the set. Every number is looked up a constant number of times, so the whole pass is O(n).


Why skip numbers whose predecessor is in the set?

If num - 1 is present, num sits inside a run that will be counted from its start. Counting from num as well would walk the same run again; for a sorted input of n values that turns the algorithm into O(n²).


Why not just sort the array?

Sorting and scanning for runs works but takes O(n log n) time. The hash set reaches O(n) by looking neighbours up instead of placing them next to each other.