Kazi Rahamatullah
Kazi Rahamatullah
AboutProjectsBlogContact
UsesBooks
ResumeView CV
Kazi Rahamatullah

© Copyright 2026 Kazi Rahamatullah

AboutProjectsBlogBooksUses
Twitter/XGitHubProduct HuntCodeSandbox
Back to Blog
DSAHash TablesHash SetsData Structures

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.

Jul 4, 20264 min read

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

#Section
1Set vs array
2Interactive visualizer
3Full implementation
4When to use
5Real-life examples

1. Set vs array

OperationArrayHash set
ContainsO(n) scanO(1) average
Add uniqueO(n) check + pushO(1) average
RemoveO(n) find + spliceO(1) average
DuplicatesAllowedRejected
plaintext
Input:  ["react", "node", "react", "typescript", "node"]
Set:    { react, node, typescript }   ← 3 unique

Back to index


2. Interactive visualizer

Hash Sets — Unique Keys

Store unique values with O(1) add, delete, and membership checks — deduplication and visited tracking.

[0]
empty
[1]
empty
[2]
empty
[3]
empty
[4]
empty
[5]
empty
[6]
empty
Capacity: 7Load: 0/7Type: Set

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

Back to index


3. Full implementation

js
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

js
// ── 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']

Back to index


4. When to use

Use caseWhy hash set
DeduplicationReject duplicates in O(1)
Visited trackingBFS/DFS — mark nodes seen
Permission checkRole in allowed set?
Two-sum / intersectionO(n) lookup for complement

Interview Answer

"Set vs hash map?"

A set stores keys only — membership tests. A map stores key → value pairs. Under the hood both use hash tables; a set is a map without values.

Back to index


5. Real-life examples

ScenarioHash set role
Unique page viewsVisitor ID in set — count once per session
Graph BFS visitedNode IDs marked seen
Email blocklistO(1) "is this email blocked?"
Tag deduplicationBlog post tags stored uniquely

Unique visitor tracking

Analytics counts unique page views — visitor IDs stored in a hash set, duplicates ignored.

visitor-8821new[0]

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.

Back to index


Summary

Hash sets = unique keys with O(1) membership. Use for dedup, visited tracking, and fast contains checks.

Next reads: Hash Maps · Hash Tables

Share this article

XLinkedInFacebook
Kazi Rahamatullah

Written by

Kazi Rahamatullah

FullStack Developer

X / TwitterGitHubLinkedIn

Subscribe to my newsletter

Stay up to date and get notified when I share new contents.

No spam ever, unsubscribe anytime