128. Longest Consecutive Sequence

ansidev Posted on December 1, 2026
Problem
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) time.
Example 1:
Input: nums = [100,4,200,1,3,2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.Example 2:
Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9Example 3:
Input: nums = [1,0,1,2]
Output: 3Constraints:
0 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9
Analysis
- The key requirement is an
O(n)time algorithm, so sorting the array (O(n log n)) is not acceptable for the optimal solution. - A consecutive sequence is a run
x, x+1, x+2, ..., x+K-1. Only the smallest numberxof a run needs to be considered as a starting point: ifx-1exists in the array, thenxis not the beginning of a sequence, and its run will be discovered when we start fromx-1(or an even smaller number). - Algorithm:
- Put every number into a hash set so existence checks are
O(1). - For each number
nin the set, ifn-1is not in the set, thennstarts a sequence. Count how far the run goes (n+1,n+2, …) and keep the maximum length.
- Put every number into a hash set so existence checks are
- Each number can start at most one sequence, and the inner counting loop only visits numbers that belong to that sequence. Every number is therefore examined a constant number of times, giving
O(n)time andO(n)space.
Example walkthrough with nums = [100, 4, 200, 1, 3, 2]:
Number n | n-1 in set? | Action | Run length |
|---|---|---|---|
| 100 | No | Start from 100; 101 missing | 1 |
| 4 | Yes (3) | Skip | — |
| 200 | No | Start from 200; 201 missing | 1 |
| 1 | No | Start from 1; run 1, 2, 3, 4; 5 missing | 4 |
| 3 | Yes (2) | Skip | — |
| 2 | Yes (1) | Skip | — |
The maximum run length is 4.
Edge cases:
- Empty input → result is 0.
- Duplicates → the set deduplicates them, so they do not affect correctness or complexity.
Approaches
Approach 1
Approach
- Build a hash set containing all numbers in
nums. - Iterate over the set. For each number
n:- If
n-1is in the set, skipn— it is not the start of a sequence. - Otherwise, count upward (
n+1,n+2, …) while the numbers exist in the set, and update the longest length found.
- If
- Return the longest length.
Solutions
func longestConsecutive(nums []int) int {
numSet := make(map[int]bool, len(nums))
for _, n := range nums {
numSet[n] = true
}
longest := 0
for n := range numSet {
// Only start counting from the beginning of a sequence.
if numSet[n-1] {
continue
}
currentNum := n
currentStreak := 1
for numSet[currentNum+1] {
currentNum++
currentStreak++
}
if currentStreak > longest {
longest = currentStreak
}
}
return longest
}Complexity
- Time Complexity:
O(n). Building the set takesO(n). Each number starts at most one sequence, and the inner loop only walks through members of that sequence, so every number is examined a constant number of times. - Space Complexity:
O(n)for the hash set.