DSA Hash Sets — Unique Membership Visualized
Learn hash sets — O(1) add, delete, and has checks, duplicate rejection, interactive bucket visualizer, and deduplication use cases.
Introduction
Hash sets are hash tables that store keys only — no values. They
guarantee unique membership with O(1) average add, delete, and has.
JavaScript Set, Python set, and Java HashSet all use this pattern.
Quick index
1. Set vs array
| Operation | Array | Hash set |
|---|---|---|
| Contains | O(n) scan | O(1) average |
| Add unique | O(n) check + push | O(1) average |
| Remove | O(n) find + splice | O(1) average |
| Duplicates | Allowed | Rejected |
Input: ["react", "node", "react", "typescript", "node"]
Set: { react, node, typescript } ← 3 unique2. Interactive visualizer
Hash Sets — Unique Keys
Store unique values with O(1) add, delete, and membership checks — deduplication and visited tracking.
Hash set stores unique keys only — no duplicate values.
HashSet class
class HashSet {
constructor(capacity = 7) {
this.buckets = Array.from({ length: capacity }, () => [])
}
add(key) { /* hash + chain if needed */ }
has(key) { /* hash + scan chain */ }
}Set = hash table with keys only.
add / has
O(1) avg
Duplicates
Rejected
Use case
Dedup
3. Full implementation
class HashSet {
constructor(capacity = 7) {
this.capacity = capacity
this.buckets = Array.from({ length: capacity }, () => [])
this.count = 0
}
_hash(key) {
let hash = 0
for (let i = 0; i < key.length; i++) {
hash = (hash + key.charCodeAt(i) * (i + 1)) % this.capacity
}
return hash
}
add(key) {
const bucket = this.buckets[this._hash(key)]
if (bucket.some((e) => e === key)) return false
bucket.push(key)
this.count++
return true
}
has(key) {
return this.buckets[this._hash(key)].some((e) => e === key)
}
delete(key) {
const index = this._hash(key)
const bucket = this.buckets[index]
const idx = bucket.indexOf(key)
if (idx === -1) return false
bucket.splice(idx, 1)
this.count--
return true
}
size() {
return this.count
}
values() {
return this.buckets.flat()
}
}
function dedupe(arr) {
const set = new HashSet()
const result = []
for (const item of arr) {
if (set.add(item)) result.push(item)
}
return result
}Function calls with output
// ── Demo calls — output shown on the right ──
const set = new HashSet()
set.add('react') // → true (inserted)
set.add('node') // → true
set.add('react') // → false (duplicate)
set.has('react') // → true
set.has('vue') // → false
set.delete('node') // → true
set.size() // → 1
dedupe(['a', 'b', 'a', 'c']) // → ['a', 'b', 'c']4. When to use
| Use case | Why hash set |
|---|---|
| Deduplication | Reject duplicates in O(1) |
| Visited tracking | BFS/DFS — mark nodes seen |
| Permission check | Role in allowed set? |
| Two-sum / intersection | O(n) lookup for complement |
5. Real-life examples
| Scenario | Hash set role |
|---|---|
| Unique page views | Visitor ID in set — count once per session |
| Graph BFS visited | Node IDs marked seen |
| Email blocklist | O(1) "is this email blocked?" |
| Tag deduplication | Blog post tags stored uniquely |
Unique visitor tracking
Analytics counts unique page views — visitor IDs stored in a hash set, duplicates ignored.
New visitor ID hashed and added to set.
Real-world implementation
class UniqueVisitors {
constructor() { this.seen = new Set() }
track(visitorId) {
const isNew = !this.seen.has(visitorId)
if (isNew) this.seen.add(visitorId)
return isNew
}
count() { return this.seen.size }
}Full working code for the scenario above.
Function calls with output
| // ── Function calls with output ── | |
| analytics.track("visitor-8821") | → true (new visitor) |
Side output shows return value after each call — step through to trace execution.
Summary
Hash sets = unique keys with O(1) membership. Use for dedup, visited tracking, and fast contains checks.
Next reads: Hash Maps · Hash Tables
Subscribe to my newsletter
Stay up to date and get notified when I share new contents.
No spam ever, unsubscribe anytime