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

# Single Number Python Solution with Tests

> Tested Python solution for LeetCode 136 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 136, Easy. Topics: Array, Bit Manipulation. [View on LeetCode](https://leetcode.com/problems/single-number/description/).

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

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

## Problem

Given a **non-empty** array of integers `nums`, every element appears *twice* except for one. Find that single one.

You must implement a solution with a linear runtime complexity and use only constant extra space.

### Examples

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

```
Input: nums = [4,1,2,1,2]
Output: 4
```

```
Input: nums = [1]
Output: 1
```

### Constraints

* 1 \<= nums.length \<= 3 \* 10^4
* -3 \* 10^4 \<= nums\[i] \<= 3 \* 10^4
* Each element in the array appears twice except for one element which appears only once.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from functools import reduce
from operator import xor


class Solution:
    # Time: O(n)
    # Space: O(1)
    def single_number(self, nums: list[int]) -> int:
        return reduce(xor, nums)
```

## Complexity

| Time | Space |
| ---- | ----- |
| O(n) | O(1)  |

## Tags

[Grind](/catalog/grind), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
