> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Longest Consecutive Sequence Python Solution

> Tested Python solution for LeetCode 128 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 128, Medium. Topics: Array, Hash Table, Union Find. [View on LeetCode](https://leetcode.com/problems/longest-consecutive-sequence/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 128   # by problem number
lcpy gen -s longest_consecutive_sequence   # by problem name
```

## 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

```
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.
```

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

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

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/longest_consecutive_sequence/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/longest_consecutive_sequence/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n) - each number visited at most twice (once as start, once as continuation)
    # Space: O(n) - hash set storage
    def longest_consecutive(self, nums: list[int]) -> int:
        """
        Find longest consecutive sequence using hash set.

        Example: nums = [100, 4, 200, 1, 3, 2]

        Step 1: Create set {100, 4, 200, 1, 3, 2}

        Step 2: For each number, check if it's sequence start (num-1 not in set):

        num=100: 99 not in set → START sequence
        100 → 101 not in set → length=1

        num=4: 3 in set → SKIP (not start)

        num=200: 199 not in set → START sequence
        200 → 201 not in set → length=1

        num=1: 0 not in set → START sequence
        1 → 2 in set → 2 → 3 in set → 3 → 4 in set → 4 → 5 not in set
        Sequence: [1,2,3,4] → length=4 ✓

        Result: max(1, 1, 4) = 4
        """
        if not nums:
            return 0

        num_set = set(nums)
        max_length = 0

        for num in num_set:
            # Only start counting from the beginning of a sequence
            if num - 1 not in num_set:
                current_num = num
                current_length = 1

                # Count consecutive numbers
                while current_num + 1 in num_set:
                    current_num += 1
                    current_length += 1

                max_length = max(max_length, current_length)

        return max_length
```

## Complexity

| Time                                                                           | Space                   |
| ------------------------------------------------------------------------------ | ----------------------- |
| O(n) - each number visited at most twice (once as start, once as continuation) | O(n) - hash set storage |

## Tags

[Grind](/catalog/grind), [Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode), [AlgoMaster 75](/catalog/algo-master-75).
