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

# First Unique Number Python Solution with Tests

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

LeetCode 1429, [Medium](/catalog/medium). Topics: [Design](/catalog/topics/design), [Queue](/catalog/topics/queue), [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Data Stream](/catalog/topics/data-stream). [View on LeetCode](https://leetcode.com/problems/first-unique-number/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 1429   # by problem number
lcpy gen -s first_unique_number   # by problem name
```

## Problem

You have a queue of integers, you need to retrieve the first unique integer in the queue.

Implement the `FirstUnique` class:

* `FirstUnique(int[] nums)` Initializes the object with the numbers in the queue.
* `int showFirstUnique()` returns the value of **the first unique** integer of the queue, and returns **-1** if there is no such integer.
* `void add(int value)` insert value to the queue.

### Examples

```
Input
["FirstUnique", "showFirstUnique", "add", "showFirstUnique", "add", "showFirstUnique", "add", "showFirstUnique"]
[[[2, 3, 5]], [], [5], [], [2], [], [3], []]
Output
[null, 2, null, 2, null, 3, null, -1]
```

```
Input
["FirstUnique", "showFirstUnique", "add", "add", "add", "add", "add", "showFirstUnique"]
[[[7, 7, 7, 7, 7, 7]], [], [7], [3], [3], [7], [17], []]
Output
[null, -1, null, null, null, null, null, 17]
```

```
Input
["FirstUnique", "showFirstUnique", "add", "showFirstUnique"]
[[[809]], [], [809], []]
Output
[null, 809, null, -1]
```

### Constraints

* `1 <= nums.length <= 10^5`
* `1 <= nums[i] <= 10^8`
* `1 <= value <= 10^8`
* At most `50000` calls will be made to `showFirstUnique` and `add`.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter, deque


class FirstUnique:
    # Time: __init__ O(n), show_first_unique amortized O(1), add O(1)
    # Space: O(n)
    def __init__(self, nums: list[int]) -> None:
        self.counts: Counter[int] = Counter(nums)
        self.queue: deque[int] = deque(nums)

    def show_first_unique(self) -> int:
        while self.queue and self.counts[self.queue[0]] != 1:
            self.queue.popleft()
        return self.queue[0] if self.queue else -1

    def add(self, value: int) -> None:
        self.counts[value] += 1
        self.queue.append(value)
```

## Complexity

| Time | Space |
| - | - |
| **init** O(n), show\_first\_unique amortized O(1), add O(1) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.