What is a HashSet?
A HashSet is similar to a HashMap, but with one key difference: it stores only unique keys without associated values. It’s perfect when you need to track whether elements exist in a collection without caring about additional data.If you’re new to hash-based data structures, we recommend reading the HashMap documentation first to understand hashing fundamentals and collision handling.
Try the Interactive Visualizer
Experiment with HashSet operations in real-time:HashSet Visualizer
Add unique values, attempt duplicates, remove entries, and observe collision handling with visual animations.
HashSet vs HashMap
HashMap
Stores key-value pairs
Use when you need associated data
{"Alice": 25, "Bob": 30}Use when you need associated data
HashSet
Stores unique keys only
Use when you only care about membership
{"Alice", "Bob", "Charlie"}Use when you only care about membership
Core Operations
The DS Visualizer HashSet uses an array of size 10 with chaining for collision resolution. The hash function computeskey % 10 for numeric keys.
Add Operation
Inserts a unique key into the set:Time Complexity: O(1) average case with good hash distribution, O(n) worst case where n = entries in the bucket
Remove Operation
Deletes a key from the set:Has/Contains Operation
Checks if a key exists in the set:Implementation Details
Entry Class
Unlike HashMap, HashSet entries only store keys:Hash Function
The visualizer uses a simple modulo-based hash for integers:- Example Hashing
- Collision Handling
Helper Method
Visual Structure
The HashSet visualizer displays:Time & Space Complexity
Add
Average: O(1)
Worst: O(n)
Worst: O(n)
Remove
Average: O(1)
Worst: O(n)
Worst: O(n)
Contains
Average: O(1)
Worst: O(n)
Worst: O(n)
The O(1) average case assumes a good hash function that distributes keys uniformly across buckets. Poor distribution leads to long chains and degraded performance.
Common Use Cases
HashSets are ideal for:1. Duplicate Detection
2. Membership Testing
3. Removing Duplicates
4. Set Operations
Collision Resolution: Chaining
Like HashMap, this HashSet uses chaining to handle collisions:- Each bucket contains a
LinkedList<Entry> - Keys that hash to the same index are stored in the same list
- Operations traverse the list to find specific keys
Try the visualizer with keys like 5, 15, 25, 35 - they all hash to index 5, demonstrating collision handling through chaining.
Interactive Learning
Use the live visualizer to:- Add unique values and watch where they’re placed
- Attempt duplicates and observe they’re rejected
- Create collisions using numbers like 3, 13, 23 (all hash to index 3)
- Remove entries and see the structure update
- Test membership by checking if values exist
HashSet vs Other Data Structures
| Feature | HashSet | Array | Linked List | BST |
|---|---|---|---|---|
| Search | O(1) avg | O(n) | O(n) | O(log n) |
| Insert | O(1) avg | O(1)* | O(1)** | O(log n) |
| Delete | O(1) avg | O(n) | O(n) | O(log n) |
| Duplicates | No | Yes | Yes | No*** |
| Ordered | No | Yes | Yes | Yes |
**At head/tail only
***Depends on implementation
Implementation in the Visualizer
The DS Visualizer uses a React hook (useHashMap) that powers both HashMap and HashSet:
The visualizer enforces integer keys for HashSet to demonstrate numeric hashing clearly. Production HashSets typically accept any hashable type.
Related Data Structures
HashMap
Store key-value pairs instead of just keys
Binary Search Tree
Ordered alternative with O(log n) operations
Key Takeaways
- HashSets store unique keys only (no associated values)
- Operations are O(1) average time (add, remove, contains)
- Duplicates are automatically prevented without errors
- Uses chaining to resolve hash collisions
- Perfect for membership testing and duplicate detection
- Simpler than HashMap when you don’t need key-value associations
- No ordering guarantees (unlike sorted sets or trees)