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

# Design HashSet Python Solution with Tests

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

LeetCode 705, Easy. Topics: Array, Hash Table, Linked List, Design, Hash Function. [View on LeetCode](https://leetcode.com/problems/design-hash-set/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 705   # by problem number
lcpy gen -s design_hash_set   # by problem name
```

## Problem

Design a HashSet without using any built-in hash table libraries.

Implement the `MyHashSet` class:

* `void add(key)` Inserts the value `key` into the HashSet.
* `bool contains(key)` Returns whether the value `key` exists in the HashSet or not.
* `void remove(key)` Removes the value `key` in the HashSet. If `key` does not exist in the HashSet, do nothing.

### Examples

```
Input
["MyHashSet", "add", "add", "contains", "contains", "add", "contains", "remove", "contains"]
[[], [1], [2], [1], [3], [2], [2], [2], [2]]
Output
[null, null, null, true, false, null, true, null, false]

Explanation
MyHashSet myHashSet = new MyHashSet();
myHashSet.add(1);      // set = [1]
myHashSet.add(2);      // set = [1, 2]
myHashSet.contains(1); // return True
myHashSet.contains(3); // return False, (not found)
myHashSet.add(2);      // set = [1, 2]
myHashSet.contains(2); // return True
myHashSet.remove(2);   // set = [1]
myHashSet.contains(2); // return False, (already removed)
```

### Constraints

* 0 \<= key \<= 10^6
* At most 10^4 calls will be made to `add`, `remove`, and `contains`.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class MyHashSet:
    # Time: O(1)
    # Space: O(n)
    def __init__(self) -> None:
        # Problem guarantees 0 <= key <= 10^6; a direct-address table is simplest.
        self._present: list[bool] = [False] * 1_000_001

    # Time: O(1)
    # Space: O(1)
    def add(self, key: int) -> None:
        self._present[key] = True

    # Time: O(1)
    # Space: O(1)
    def remove(self, key: int) -> None:
        self._present[key] = False

    # Time: O(1)
    # Space: O(1)
    def contains(self, key: int) -> bool:
        return self._present[key]
```

## Complexity

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

## Tags

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