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

Implement MyHashSet class:

  • MyHashSet() Initializes the MyHashSet object.
  • 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.

Your implementation should handle typical hash set operations efficiently.

Input & Output

Example 1 — Basic Operations
$ Input: operations = ["MyHashSet", "add", "add", "contains", "contains", "add", "contains", "remove", "contains"], values = [[], [1], [2], [1], [3], [2], [2], [2], [2]]
Output: [null, null, null, true, false, null, true, null, false]
💡 Note: Initialize empty set, add 1 and 2, check if 1 exists (true), check if 3 exists (false), add 2 again (no change), check if 2 exists (true), remove 2, check if 2 exists (false)
Example 2 — Remove Non-existent
$ Input: operations = ["MyHashSet", "add", "remove", "contains"], values = [[], [5], [10], [5]]
Output: [null, null, null, true]
💡 Note: Add 5 to set, try to remove 10 (doesn't exist, no change), check if 5 exists (true)
Example 3 — Duplicate Adds
$ Input: operations = ["MyHashSet", "add", "add", "contains"], values = [[], [7], [7], [7]]
Output: [null, null, null, true]
💡 Note: Add 7, add 7 again (no change since sets don't allow duplicates), check if 7 exists (true)

Constraints

  • 0 ≤ key ≤ 106
  • At most 104 calls will be made to add, remove, and contains

Visualization

Tap to expand
Design HashSet - Hash Table with Chaining INPUT Operations: ["MyHashSet", "add", "add", "contains", "contains", "add", "contains", "remove", "contains"] vals: [[],[1],[2],[1],[3],... HashSet Structure: 0 1 2 ... 1 2 12 Bucket index = key % size Collisions handled by chaining Keys: 1, 2, 3 1 2 3 ALGORITHM STEPS 1 Initialize HashSet Create bucket array (size 1000) 2 add(key) Method hash = key % size Add to bucket if not exists 3 contains(key) Method Find bucket, search in chain Return true/false 4 remove(key) Method Find bucket, delete from chain Execution Trace: Op State Return add(1) {1} null add(2) {1,2} null contains(1) {1,2} true contains(3) {1,2} false remove(2) {1} null contains(2) {1} false FINAL RESULT Final HashSet State: {1} 0 empty 1 1 2 empty Output Array: [null, null, null, true, false, null, true, null, false] Summary: - add/remove return null - contains(1) = true (exists) - contains(3) = false (never added) - contains(2) = false (removed) OK Key Insight: Hash Table with Chaining uses an array of linked lists (buckets) to handle collisions. Each key is mapped to a bucket using hash(key) = key % size. Multiple keys in the same bucket form a chain. This provides O(1) average time for add, remove, and contains operations. TutorialsPoint - Design HashSet | Hash Table with Chaining
Asked in
Google 35 Facebook 28 Amazon 25 Microsoft 22
125.0K Views
Medium Frequency
~25 min Avg. Time
1.8K 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