Best Solution for Arrays, HashSet 🚀 in C++, Python and Java || 100% working
💡 IntuitionTo design a HashSet, we can use hashing to distribute keys into different buckets. By using a vector of vectors, we handle collisions through separat
# 💡 Intuition  
To design a HashSet, we can use **hashing** to distribute keys into different buckets. By using a **vector of vectors**, we handle collisions through **separate chaining**.

# 🚀 Approach  
- Use a **hash function** (`key % n`) to map keys to buckets.  
- **Add**: Insert the key if it doesn’t already exist.  
- **Remove**: Find and delete the key from the bucket.  
- **Contains**: Check if the key exists in the bucket.

# ⏱️ Complexity  
- **Time Complexity:**  
  - `add`, `remove`, `contains`: Average case **O(1)**, worst case **O(n)** (when many keys hash to the same bucket)  
- **Space Complexity:**  
  - **O(n)** for storing keys  

# 💻 Code

```cpp []
class MyHashSet {
public:
    int n = 10000;
    vector<vector<int>> arr;
    MyHashSet() {
        arr.resize(n);  
    }
    
    void add(int key) {
        int idx = key % n;
        for (int x : arr[idx]) {
            if (x == key) return;
        }
        arr[idx].push_back(key);
    }
    
    void remove(int key) {
        int idx = key % n;
        for (int i = 0; i < arr[idx].size(); i++) {
            if (arr[idx][i] == key) {
                arr[idx].erase(arr[idx].begin() + i);
                return;
            }
        }
    }
    
    bool contains(int key) {
        int idx = key % n;
        for (int x : arr[idx]) {
            if (x == key) return true;
        }
        return false;
    }
};
```

```python []
class MyHashSet:
    def __init__(self):
        self.n = 10000
        self.arr = [[] for _ in range(self.n)]

    def add(self, key: int) -> None:
        idx = key % self.n
        if key not in self.arr[idx]:
            self.arr[idx].append(key)

    def remove(self, key: int) -> None:
        idx = key % self.n
        if key in self.arr[idx]:
            self.arr[idx].remove(key)

    def contains(self, key: int) -> bool:
        idx = key % self.n
        return key in self.arr[idx]
```

```java []
class MyHashSet {
    int n = 10000;
    List<Integer>[] arr;

    public MyHashSet() {
        arr = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            arr[i] = new ArrayList<>();
        }
    }

    public void add(int key) {
        int idx = key % n;
        if (!arr[idx].contains(key)) {
            arr[idx].add(key);
        }
    }

    public void remove(int key) {
        int idx = key % n;
        arr[idx].remove(Integer.valueOf(key));
    }

    public boolean contains(int key) {
        int idx = key % n;
        return arr[idx].contains(key);
    }
}
```
<img src="https://assets.leetcode.com/users/images/7b864aef-f8a2-4d0b-a376-37cdcc64e38c_1735298989.3319144.jpeg" alt="upvote" width="150px">

# Connect with me on LinkedIn for more insights! 🌟 Link in bio
['Array', 'Hash Table', 'Linked List', 'Design', 'Hash Function', 'Python', 'C++', 'Java', 'Python3']