Design HashSet - Complete Solution Guide
Design HashSet is LeetCode problem 705, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Design a HashSet without using any built-in hash table libraries. Implement 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. Example 1: Input ["MyHashSet", "add", "add", "contains", "contains", "add", "contains", "remove", "contains"] [[], [1], [2], [1], [3], [2], [2], [2], [2]] Output [null,
Detailed Explanation
The problem requires you to implement a `MyHashSet` class that mimics the behavior of a HashSet data structure, without using built-in hash table libraries. A HashSet is a data structure that stores unique elements. The class should support three operations: `add(key)` to insert a key, `remove(key)` to delete a key, and `contains(key)` to check if a key exists in the set. The input `key` is a non-negative integer within the range [0, 10^6]. The number of calls to the methods is at most 10^4.
Solution Approach
The provided solutions implement the HashSet using different approaches. The Java and C++ solutions use a boolean array where the index represents the key, and the value represents whether the key is present in the set. The Python solution uses a simple hashing method with separate chaining. The C solution uses a dynamically allocated array to store the keys. When adding a key, we set the corresponding boolean array element to true (Java/C++). To remove, we set it to false. To check containment, we simply look up the value in the boolean array. The Python implementation uses a modulo operator for simple hashing to distribute keys into buckets, followed by adding, removing, or checking the list at the target bucket. The C implementation directly stores elements in an array, growing the array's capacity as needed.
Step-by-Step Algorithm
- Step 1: Initialize the HashSet. For Java/C++, create a boolean array of size 1000001. For Python, create a list of lists (buckets). For C, allocate an array to store keys.
- Step 2: `add(key)`: For Java/C++, set `array[key] = true`. For Python, calculate the index using `key % size`, and append the key to the list at `table[index]` if it's not already present. For C, add the key to the key array, reallocating the array if it's full.
- Step 3: `remove(key)`: For Java/C++, set `array[key] = false`. For Python, calculate the index using `key % size`, and remove the key from the list at `table[index]` if it's present. For C, find the key in the keys array and replace it with the last element to effectively remove it.
- Step 4: `contains(key)`: For Java/C++, return `array[key]`. For Python, calculate the index using `key % size`, and return `True` if the key is present in the list at `table[index]`, otherwise `False`. For C, iterate through the key array and return `True` if the key is found, `False` otherwise.
- Step 5: `free()`: In the C implementation, free the dynamically allocated memory for the keys array, and then the HashSet object itself.
Key Insights
- Insight 1: Since we can't use built-in hash table libraries, we need to choose an implementation strategy. One efficient approach is to use a boolean array (or a vector of booleans) due to the relatively small key range [0, 10^6].
- Insight 2: Alternatively, we could use a separate chaining approach with a hash function and a list (or linked list) for each bucket to handle collisions. This approach is more space-efficient if the number of distinct keys is significantly smaller than the maximum possible key value.
- Insight 3: Edge cases related to removing non-existent elements need to be handled to avoid errors or unexpected behavior.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, Linked List, Design, Hash Function.
Companies
Asked at: Wix.