> ## 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 III Python Solution with Tests

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

LeetCode 260, Medium. Topics: Array, Bit Manipulation. [View on LeetCode](https://leetcode.com/problems/single-number-iii/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 260   # by problem number
lcpy gen -s single_number_iii   # by problem name
```

## Problem

Given an integer array `nums`, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in **any order**.

You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.

### Examples

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

**Explanation:** \[5, 3] is also a valid answer.

```
Input: nums = [-1,0]
Output: [-1,0]
```

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

### Constraints

* 2 \<= nums.length \<= 3 \* 10^4
* -2^31 \<= nums\[i] \<= 2^31 - 1
* Each integer in nums will appear twice, only two integers will appear once.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def single_number(self, nums: list[int]) -> list[int]:
        # XOR all numbers - the result is xor of the two unique numbers
        xor_all = 0
        for num in nums:
            xor_all ^= num

        # Find rightmost set bit (differentiating bit between the two unique numbers)
        diff_bit = xor_all & -xor_all

        # Partition numbers based on the differentiating bit
        # and XOR each partition to find the unique numbers
        num1, num2 = 0, 0
        for num in nums:
            if num & diff_bit:
                num1 ^= num
            else:
                num2 ^= num

        return [num1, num2]
```

## Complexity

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

## Tags

[NeetCode All](/catalog/neetcode), [AlgoMaster 75](/catalog/algo-master-75).
