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

Your mission is to implement a MyHashSet class from scratch that provides the core functionality of a hash set data structure. A HashSet is a collection that stores unique elements and provides fast insertion, deletion, and lookup operations.

Implement the following methods:
add(key) - Inserts the value key into the HashSet
contains(key) - Returns true if the value key exists in the HashSet, false otherwise
remove(key) - Removes the value key from the HashSet (no-op if key doesn't exist)

This problem tests your understanding of hash functions, collision handling, and fundamental data structure design principles.

Input & Output

example_1.py — Basic Operations
$ 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]
💡 Note: MyHashSet is initialized, add(1), add(2), contains(1) returns true, contains(3) returns false, add(2) does nothing since 2 already exists, contains(2) returns true, remove(2), contains(2) returns false
example_2.py — Duplicate Handling
$ Input: ["MyHashSet", "add", "add", "add", "contains"] [[], [5], [5], [5], [5]]
Output: [null, null, null, null, true]
💡 Note: Adding the same element multiple times should only store it once. The set should still contain the element after multiple add operations.
example_3.py — Remove Non-existent
$ Input: ["MyHashSet", "remove", "add", "remove", "remove", "contains"] [[], [1], [1], [1], [1], [1]]
Output: [null, null, null, null, null, false]
💡 Note: Removing a non-existent element should do nothing. After removing an element that was added, contains should return false.

Visualization

Tap to expand
Hash Set: Library Organization SystemSection A-ESection F-JSection K-OSection P-TSection U-Z101201302402502123823Operation: contains(302)1. Hash Function: 302 % 5 = 2 → Go to Section F-J2. Search only in Section F-J (3 books instead of all 7 books)Linear Search: O(n)Check every single bookHash Table: O(1) averageGo directly to right sectionTarget!
Understanding the Visualization
1
Design Hash Function
Create a function that maps book IDs to specific sections (buckets) evenly
2
Handle Collisions
When multiple books map to the same section, store them as a list within that section
3
Optimize Access
Each operation now only searches within the relevant section, not the entire library
Key Takeaway
🎯 Key Insight: Hash functions transform O(n) searches into O(1) operations by intelligently distributing data across multiple buckets, like organizing a library into sections for instant book location.

Time & Space Complexity

Time Complexity
⏱️
O(1)

Average case O(1) due to hash function distributing elements evenly across buckets. Worst case O(n) if all elements hash to same bucket

n
2n
Linear Growth
Space Complexity
O(n + k)

O(n) for storing n elements plus O(k) for k buckets array

n
2n
Linearithmic Space

Constraints

  • 0 ≤ key ≤ 106
  • At most 104 calls will be made to add, remove, and contains
Asked in
Google 43 Amazon 38 Meta 29 Microsoft 25 Apple 18
89.5K Views
High Frequency
~15 min Avg. Time
2.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen