LINEAR DATA STRUCTURES / ALGORITHM BRIEF

Hash set

A hash set stores unique values and answers 'have I seen this?' in O(1) on average.

BeginnerPhase 02 / Topic 8 of 8Mental modelComplexityEdge cases
01

Overview

A hash set stores unique values and answers 'have I seen this?' in O(1) on average. It is a hash map with keys only. Adding a value that already exists has no effect.

Sets are the simplest way to remove duplicates, check membership, and compute intersections and unions. Many O(n^2) 'does it exist' loops become O(n) by putting one side into a set.

A guest list at the door

The bouncer only needs to know whether a name is on the list, not how many times it appears or in what order it was added. Checking a name takes the same time whether the list has 10 or 10,000 names.

02

When to use it

  • Detecting duplicates or cycles (visited states).
  • Fast membership tests: is this word in the dictionary?
  • Removing duplicates while preserving first occurrence.
  • Set algebra: intersection, union, difference.
  • Visited tracking in BFS and DFS.
03

Problem patterns it solves

Duplicate detection

Recognize it when: contains duplicate, first repeated, find the duplicate.

  • 217. Contains Duplicate
  • 219. Contains Duplicate II
  • 1876. Substrings of Size Three with Distinct Characters
Membership and existence

Recognize it when: does x - 1 or target - x exist; is the word in a dictionary.

  • 128. Longest Consecutive Sequence
  • 771. Jewels and Stones
  • 139. Word Break
Set algebra

Recognize it when: common elements, elements in one but not the other.

  • 349. Intersection of Two Arrays
  • 2215. Find the Difference of Two Arrays
  • 1002. Find Common Characters
Cycle detection on states

Recognize it when: a process repeats; detect a repeated state.

  • 202. Happy Number
  • 36. Valid Sudoku
  • 957. Prison Cells After N Days
Visited set in graph search

Recognize it when: BFS / DFS where nodes or states can repeat.

  • 127. Word Ladder
  • 752. Open the Lock
  • 841. Keys and Rooms
04

Where it is used in real software

Unique visitors

Counting unique users uses a set for exact counts; at massive scale, HyperLogLog estimates the size of a set with a few kilobytes.

Blocklists and allowlists

Firewalls and spam filters check IPs and domains against sets; Bloom filters provide a memory-efficient probabilistic version.

Crawlers

Web crawlers keep a visited-URL set so each page is fetched once.

Permissions

A user's roles or scopes are a set; checking access is a membership test.

05

Key terms

add
Insert a value; no effect if already present. O(1) average.
has / contains
Membership test. O(1) average.
delete / remove
Remove a value. O(1) average.
Bloom filter
Probabilistic set: no false negatives, small chance of false positives, very little memory.
06

Using a set effectively

  1. 1
    Decide what identity means

    Numbers and strings compare by value. For pairs or coordinates, encode a string key like `${r},${c}` in JS, or use a record in Java.

  2. 2
    Check before adding when you need to detect repeats

    if (set.has(x)) duplicate found; else set.add(x).

  3. 3
    Build from an array for membership tests

    new Set(arr) is O(n); every later has() is O(1).

  4. 4
    Remove values to maintain a window

    In sliding window problems, delete the value leaving the window.

07

Happy number cycle detection

n = 2: replace n with the sum of the squares of its digits

Step 1 / 10
nNext valueSeen before?Set size after
24no1
416no2
1637no3
3758no4
5889no5
89145no6
14542no7
4220no8
204no9
4-yes: cyclestop, not happy

NOWn: 2 | Next value: 4 | Seen before?: no | Set size after: 1

4 repeats, so the process loops forever without reaching 1. The set detects the cycle in O(number of states); Floyd's fast and slow pointers can do it with O(1) space.

08

Implementation

const containsDuplicate = (nums) => new Set(nums).size !== nums.length; // 219: duplicate within distance k (set as a sliding window)function containsNearbyDuplicate(nums, k) {  const window = new Set();  for (let i = 0; i < nums.length; i++) {    if (window.has(nums[i])) return true;    window.add(nums[i]);    if (window.size > k) window.delete(nums[i - k]);  }  return false;} function isHappy(n) {  const seen = new Set();  while (n !== 1 && !seen.has(n)) {    seen.add(n);    let next = 0;    while (n > 0) {      const d = n % 10;      next += d * d;      n = Math.floor(n / 10);    }    n = next;  }  return n === 1;} // Set algebraconst a = new Set([1, 2, 3]), b = new Set([2, 3, 4]);const intersection = [...a].filter((x) => b.has(x)); // [2, 3]const union = new Set([...a, ...b]);                 // {1, 2, 3, 4}const difference = [...a].filter((x) => !b.has(x));  // [1] // Coordinates: encode as stringsconst visited = new Set();visited.add(`${2},${3}`);
09

Complexity and performance

add / has / deleteO(1) average

Same as hash map.

Build from arrayO(n)

One add per element.

Intersection of sizes n and mO(n + m)

Build one set, probe with the other.

TreeSet operationsO(log n)

Sorted, supports floor and ceiling.

10

Trade-offs

Set vs sorting for duplicates

A set gives O(n) time and O(n) space. Sorting gives O(n log n) time and O(1) extra space (if in place).

Exact vs probabilistic

A Bloom filter uses a tiny fraction of the memory of a set but can report false positives.

Hash set vs boolean array

For values in a small known range, a boolean array or bitset is faster.

11

Variants and related techniques

Sorted set (TreeSet)

Ordered iteration, floor, ceiling, and range views in O(log n).

Bitset

One bit per possible value; ideal for dense small ranges.

In-place marking

For values 1..n in an array of size n, mark presence by negating arr[value - 1] for O(1) extra space.

12

Common mistakes

  • Adding arrays or objects to a JS Set and expecting value equality.

    Fix: Sets compare objects by reference. Encode them as strings or numbers first.

  • Using a set when counts matter.

    Fix: Use a map of counts when duplicates must be counted.

  • Iterating a set while deleting in Java.

    Fix: Use an Iterator's remove(), or collect items to remove first.

13

Interview questions

Hash set vs hash map?

A set stores only keys and answers membership. A map associates each key with a value, such as a count or an index.

How would you find duplicates using O(1) extra space?

Sort in place and compare neighbors (O(n log n)), or, when values are in 1..n, mark indexes by negation, or use Floyd's cycle detection as in Find the Duplicate Number.

14

Practice problems

ProblemDifficultyWhat it trains
217. Contains DuplicateEasyBasic membership.
202. Happy NumberEasyDetect repeated states.
349. Intersection of Two ArraysEasySet algebra.
219. Contains Duplicate IIEasySet as a sliding window.
36. Valid SudokuMediumEncoded keys.
128. Longest Consecutive SequenceMediumO(n) with membership.