LeetCode 128, Medium. Topics: Array, Hash Table, Union Find. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 15 parametrized pytest cases, and a playground notebook:
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.
Examples
Constraints
- 0 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Grind, Blind 75, NeetCode 150, NeetCode 250, NeetCode All, AlgoMaster 75. Last modified on September 7, 2026