
The key takeaways
- Hash set lookup is O(1) because it maps any element to a bucket index in one hash computation and one array access, regardless of collection size.
- Collisions are handled via separate chaining (Java, C++) or open addressing (Python, Rust, C#); each strategy trades cache efficiency against iterator stability guarantees.
- Amortized O(1) inserts rely on geometric resizing: doubling capacity spreads the rehash cost evenly across all prior insertions so no single insert is expensive on average.
- Hash flooding is a real attack: Python 3.3+, Rust (SipHash 1-3), and Java 8+ (tree-chain fallback) each have mitigations baked into their runtimes.
- The interview signal is any problem hiding "have I seen this?" or "does the complement exist?" -- Two Sum and Longest Consecutive Sequence are the canonical patterns.
- Go has no built-in set: use
map[T]struct{}with a zero-byte empty struct as the value to avoid paying for a dummy stored value.
You have a list of numbers. Some are duplicates. You need to know, for any given value, whether you've already encountered it.
Your first instinct: scan the list each time. It works. It also turns your outer loop into O(n²) before you finish your coffee, and then your interviewer's face does that thing you don't want to see.
A hash set does the same lookup in O(1). Not O(log n). Not "O(n) in a good mood." Hash set time complexity stays constant on average because the work never grows with the collection size: one hash computation, one array access. Any time the question is "have I seen this?" or "is this value in the collection?", a hash set is almost certainly the right tool.

The hash set's entire job: answering "have I seen this?" in constant time. Without it, "low but never zero" becomes "certain and expensive."
Not a Map. Just Set Semantics.
A hash set and a hash map share the same foundation: a hash table. The difference is what you store. A map holds key-value pairs. A set holds only keys. Membership, not association.
Some languages make this explicit in implementation. Java's HashSet<E> literally wraps a HashMap<E, Object> with a shared dummy value called PRESENT. It is a map internally. The "set" is just a map that forgot it had values. Rust's HashSet<T> is HashMap<T, ()> under the hood. CPython's set type uses the same open-addressing table structure as dict, stripped of value slots.
The interface simplification matters. You are not asking "what's associated with X?" You are asking "does X exist?" That's a different cognitive frame, and it leads to different problem patterns.
How It Works Internally
The Hash Table Foundation
A hash set maintains an internal array of slots called buckets. To look up an element:
- Compute its hash: a deterministic function from the element to an integer.
- Map the hash to a bucket index: typically
hash & (capacity - 1), a fast bitwise AND when capacity is a power of two (which all major implementations enforce). - Check whether that bucket contains the element.
element → hash(element) → index = hash & mask → check buckets[index]That's why lookup is O(1). The work is bounded: one hash computation, one array access. The capacity is irrelevant to the cost.

One step to the hash. One step to the bucket. Done. The table could have a million entries or eight; the lookup cost is the same.
The Collision Problem
Hash functions are not injective. Two different elements can produce the same bucket index. Collisions are inevitable, like the worst kind of party where two people show up as the same costume but the venue only has one hook for each outfit. The interesting question is how implementations handle them.
Separate chaining
Each bucket holds a pointer to a linked list. Colliding elements get chained together at the same index.
Bucket array:
[0] → null
[1] → ["apple"] → null
[2] → ["banana"] → ["bongo"] → null ← collision at index 2
[3] → nullEvery populated-bucket lookup chases a pointer off the main array into fragmented heap memory, killing cache locality. Each link is a cache miss waiting to happen.
C++'s std::unordered_set uses separate chaining. The standard requires that references to elements remain stable after insertions, which rules out open addressing (elements can't move on resize).
Open addressing
All elements live inside the array itself. No linked lists. When a collision occurs, the algorithm probes for an alternative slot using a probing sequence.
Bucket array (linear probing, capacity = 8):
[0]: empty
[1]: "apple" ← hashes to index 1
[2]: "banana" ← hashes to index 2
[3]: "bongo" ← also hashes to index 2, placed at index 3
[4]: emptyCPython's set uses a hybrid probing strategy: it starts with a short run of linear probes (cache-friendly, exploits spatial locality), then falls back to a pseudo-random probe sequence derived from the top 5 bits of the hash value. This prevents primary clustering, the phenomenon where a dense run of occupied slots forces increasingly long probe sequences.
Rust's HashSet (backed by the hashbrown crate) uses Swiss table design: Robin Hood hashing with quadratic probing and SIMD lookup. A 16-byte control group holds the top 7 bits of each element's hash as metadata, and a single SIMD instruction can compare all 16 slots at once. This makes lookups extremely cache-efficient even at high load factors.
Go's bucketed hybrid
Go's map uses neither pure strategy. Each bucket holds exactly 8 key-value pairs, stored with all keys packed together followed by all values (eliminating alignment padding). When a bucket fills, Go allocates an overflow bucket and chains it. The top byte of each key's hash is stored in a tophash array inside the bucket, so Go can eliminate most full-key comparisons with a fast byte check before touching the actual key.

Separate chaining: each collision extends a linked list, but every pointer hop is a potential cache miss. Open addressing: collisions probe forward in the array, staying cache-friendly at the cost of needing tombstones when you delete.
The Load Factor and Resize
The load factor is the ratio of stored elements to total capacity. Think of it like an apartment. Put enough stuff in and finding anything requires climbing over everything. Same idea, except your apartment is an array and the stuff is hashed integers. As the load factor rises, collisions become more likely and probe sequences get longer. Every implementation defines a threshold:
| Implementation | Strategy | Load factor threshold |
|---|---|---|
CPython set |
Open addressing (hybrid probe) | 2/3 (~66%) |
Java HashSet |
Separate chaining | 0.75 |
C++ unordered_set |
Separate chaining | 1.0 (implementation-defined) |
Rust HashSet (hashbrown) |
Open addressing (Robin Hood + SIMD) | ~0.875 |
Go map |
Bucketed hybrid | 6.5 elements/bucket |
When the threshold is crossed, the table allocates a new array (typically double the current capacity) and rehashes every existing element into the new positions. CPython specifically quadruples the capacity when the set has fewer than 50,000 active entries and doubles it above that threshold.
The Amortized Resize Argument
Doubling looks expensive. Adding one element can trigger an O(n) rehash. How can add() possibly claim O(1)?
The doubling strategy guarantees that the total cost of n insertions is O(n), so the amortized cost per insertion is O(1).
Here is the accounting. Consider a table that doubles every time it fills. When it doubles at size n, it rehashes n elements at cost O(n). But at that moment, at least n/2 of those elements were inserted after the previous resize. They each paid 2 units of "amortized credit" at insertion time. Those n/2 elements have accumulated n credits total, exactly enough to cover the O(n) rehash. After the resize, the credit balance returns to zero.
The key is geometric growth. If the table grew by a fixed constant (say +10 slots each time), you'd resize O(n) times for n insertions and the total cost would be O(n²). Doubling gives O(log n) resizes total, each completely covered by pre-accumulated amortized credit. This is why "but add() can be O(n) sometimes" is technically true and practically irrelevant.
The Hash Function Is Not Decoration
The O(1) guarantee is average case, not worst case. If every element hashes to the same bucket, lookup degrades to O(n) and insertion degrades to O(n²). This is not the kind of quadratic you debug happily.
This is not hypothetical. In 2011, researchers demonstrated hash flooding attacks on PHP and Java web servers. The trick: craft HTTP POST bodies with keys engineered to all land in the same bucket. The server faithfully inserts them, each one slightly slower than the last, until the CPU pegs at 100% doing basically nothing useful. Classic denial of service, powered entirely by the math of bad hash distribution.
Modern runtimes responded directly:
- Python 3.3+: hash randomization is on by default. A random seed is mixed into string and bytes hashes at interpreter startup. The same key hashes differently in each process, so an attacker cannot precompute a collision set.
- Rust: uses SipHash 1-3 by default, a keyed hash function resistant to collision attacks. You can swap to a faster unkeyed hasher (AHash, FNV) if you control the inputs and don't need the security guarantee.
- Java 8+:
HashMapconverts chains exceeding 8 elements into red-black trees, bounding worst-case lookup at O(log n) even under adversarial inputs.
Hash Set Time Complexity: Core Operations
| Operation | Average case | Worst case | Space |
|---|---|---|---|
add(x) |
O(1) amortized | O(n) | O(1) |
contains(x) |
O(1) | O(n) | O(1) |
remove(x) |
O(1) amortized | O(n) | O(1) |
size() |
O(1) | O(1) | O(1) |
union(A, B) |
O(n + m) | O(n + m) | O(n + m) |
intersection(A, B) |
O(min(n, m)) | O(n x m) | O(min(n, m)) |
difference(A, B) |
O(n) | O(n x m) | O(n) |
| Build from iterable | O(n) amortized | O(n²) | O(n) |
Why contains() is O(1): one hash computation, one array access. The hash maps directly to a bucket index. Collisions add a constant number of additional probes at normal load factors.
Why add() is O(1) amortized: same cost as contains(), plus the amortized resize credit described above. An individual add() can be O(n) when it triggers a rehash, but that cost is paid in advance across all previous insertions.
Why remove() in open addressing is subtle: you cannot simply clear the bucket. A later lookup for a different element that probed past this slot would terminate too early and report a false negative. The slot looks empty, so the probe stops. But the element you want is still sitting one slot further along, patiently waiting to be found. The fix is a tombstone: a sentinel meaning "slot was occupied, keep probing." Tombstones count toward the load factor (fill in CPython tracks active entries plus tombstones), so a table heavy with deletions can trigger a resize even when the active entry count is low. CPython tracks fill separately from used precisely to handle this.

Without the tombstone: delete "banana," and "bongo" becomes unfindable even though it's sitting right there. The tombstone says "I was here, keep looking."
Why intersection(A, B) is O(min(n, m)): iterate the smaller set and check membership in the larger. Each check is O(1). The cost is proportional to the smaller set's size, not the larger's.
Implementations
s: set[int] = set()
s.add(1)
s.add(2)
s.add(2) # duplicate, silently ignored
print(2 in s) # True, O(1)
print(3 in s) # False, O(1)
s.discard(2) # remove if present; no error if absent
s.remove(1) # remove; raises KeyError if absent
# Set literals and operations
a = {1, 2, 3}
b = {2, 3, 4}
print(a | b) # {1, 2, 3, 4} union
print(a & b) # {2, 3} intersection
print(a - b) # {1} difference
print(a ^ b) # {1, 4} symmetric difference
# Build from iterable, duplicates collapse
deduped = set([1, 2, 2, 3, 3, 3]) # {1, 2, 3}CPython's set starts with 8 buckets. Its setentry struct stores a 64-bit hash alongside a 64-bit pointer to the element. The table uses the hybrid probing strategy described above and resizes when fill exceeds 2/3 of capacity.
s = set()
s.add(1)
s.add(2)
s.add(2)
print 2 in s # True
print 3 in s # False
s.discard(2)
# No set literals in Python 2, use the constructor
a = set([1, 2, 3])
b = set([2, 3, 4])
print a | b # set([1, 2, 3, 4])
print a & b # set([2, 3])
print a - b # set([1])The set type has been available since Python 2.4. The frozenset type (immutable, hashable) exists in both Python 2 and 3 and can itself be a member of a set.
const s = new Set();
s.add(1);
s.add(2);
s.add(2); // ignored; Set.add returns the set itself
console.log(s.has(2)); // true
console.log(s.has(3)); // false
s.delete(2);
console.log(s.size); // 1
// Set preserves insertion order for iteration
for (const value of s) {
console.log(value);
}
// Dedup an array
const deduped = [...new Set([1, 2, 2, 3, 3, 3])]; // [1, 2, 3]
// Set operations (no native methods; build them manually)
const a = new Set([1, 2, 3]);
const b = new Set([2, 3, 4]);
const union = new Set([...a, ...b]);
const intersection = new Set([...a].filter(x => b.has(x)));
const difference = new Set([...a].filter(x => !b.has(x)));JavaScript's Set is spec'd as an ordered collection: iteration follows insertion order. The ECMAScript spec does not mandate an underlying hash table, but all major engines (V8, SpiderMonkey, JavaScriptCore) use one.
const s = new Set<number>();
s.add(1);
s.add(2);
s.add(2);
console.log(s.has(2)); // true
s.delete(2);
function deduplicate<T>(items: T[]): T[] {
return [...new Set(items)];
}
function intersection<T>(a: Set<T>, b: Set<T>): Set<T> {
return new Set([...a].filter(item => b.has(item)));
}
function union<T>(a: Set<T>, b: Set<T>): Set<T> {
return new Set([...a, ...b]);
}
function difference<T>(a: Set<T>, b: Set<T>): Set<T> {
return new Set([...a].filter(item => !b.has(item)));
}TypeScript adds generic type parameters over JavaScript's Set. The runtime behavior is identical; the types are erased at compile time.
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;
Set<Integer> s = new HashSet<>();
s.add(1);
s.add(2);
s.add(2); // returns false, element already present
System.out.println(s.contains(2)); // true
s.remove(2);
// Set operations (methods mutate the receiver)
Set<Integer> a = new HashSet<>(Set.of(1, 2, 3));
Set<Integer> b = new HashSet<>(Set.of(2, 3, 4));
Set<Integer> union = new HashSet<>(a);
union.addAll(b); // {1, 2, 3, 4}
Set<Integer> intersection = new HashSet<>(a);
intersection.retainAll(b); // {2, 3}
Set<Integer> difference = new HashSet<>(a);
difference.removeAll(b); // {1}
// Insertion-order iteration
Set<Integer> ordered = new LinkedHashSet<>(Set.of(3, 1, 2));
// Sorted iteration, uses red-black tree, O(log n) per op
Set<Integer> sorted = new TreeSet<>(Set.of(3, 1, 2)); // iterates as 1, 2, 3HashSet is backed by HashMap<E, Object> with all values set to a shared PRESENT constant. This wastes one object reference per entry compared to a purpose-built implementation but keeps the codebase simple. LinkedHashSet adds a doubly-linked list for stable iteration order at minimal overhead. TreeSet swaps the hash table for a red-black tree, giving O(log n) per operation and sorted iteration.
#include <unordered_set>
#include <set>
#include <algorithm>
#include <iterator>
// Hash set, O(1) average, separate chaining
std::unordered_set<int> s;
s.insert(1);
s.insert(2);
s.insert(2); // no-op; insert returns pair<iterator, bool>
std::cout << s.count(2); // 1 (present)
std::cout << s.count(3); // 0 (absent)
s.erase(2);
// Ordered set, O(log n), red-black tree
std::set<int> ordered = {3, 1, 2}; // iterates as 1, 2, 3
// Set operations using <algorithm> (inputs must be sorted)
std::vector<int> a = {1, 2, 3}, b = {2, 3, 4}, result;
std::set_intersection(a.begin(), a.end(), b.begin(), b.end(),
std::back_inserter(result)); // {2, 3}
std::set_union(a.begin(), a.end(), b.begin(), b.end(),
std::back_inserter(result)); // {1, 2, 3, 4}The C++ standard specifies that std::unordered_set provides O(1) average complexity for insert, lookup, and erase, but does not mandate the internal strategy. The requirement that element iterators remain stable after insertions makes open addressing impractical (elements must not move). All major implementations (libstdc++, libc++, MSVC STL) use separate chaining.
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define INITIAL_CAPACITY 64
typedef struct {
int key;
int occupied;
int deleted; /* tombstone for open addressing */
} Bucket;
typedef struct {
Bucket *buckets;
int capacity;
int size;
} HashSet;
static unsigned int hash_key(int key, int capacity) {
/* Knuth multiplicative hash */
return ((unsigned int)key * 2654435761u) % (unsigned int)capacity;
}
HashSet *hashset_create(void) {
HashSet *hs = malloc(sizeof(HashSet));
hs->capacity = INITIAL_CAPACITY;
hs->size = 0;
hs->buckets = calloc(INITIAL_CAPACITY, sizeof(Bucket));
return hs;
}
int hashset_contains(const HashSet *hs, int key) {
unsigned int index = hash_key(key, hs->capacity);
while (hs->buckets[index].occupied || hs->buckets[index].deleted) {
if (hs->buckets[index].occupied && hs->buckets[index].key == key) {
return 1;
}
index = (index + 1) % (unsigned int)hs->capacity;
}
return 0;
}
void hashset_add(HashSet *hs, int key) {
if (hashset_contains(hs, key)) {
return;
}
unsigned int index = hash_key(key, hs->capacity);
while (hs->buckets[index].occupied) {
index = (index + 1) % (unsigned int)hs->capacity;
}
hs->buckets[index].key = key;
hs->buckets[index].occupied = 1;
hs->buckets[index].deleted = 0;
hs->size++;
}
void hashset_remove(HashSet *hs, int key) {
unsigned int index = hash_key(key, hs->capacity);
while (hs->buckets[index].occupied || hs->buckets[index].deleted) {
if (hs->buckets[index].occupied && hs->buckets[index].key == key) {
hs->buckets[index].occupied = 0;
hs->buckets[index].deleted = 1; /* tombstone, not a hole */
hs->size--;
return;
}
index = (index + 1) % (unsigned int)hs->capacity;
}
}
void hashset_destroy(HashSet *hs) {
free(hs->buckets);
free(hs);
}This bare-C implementation uses linear probing with tombstones. Production use would add resizing logic (check load factor in hashset_add, double and rehash when size > capacity * 2/3). The tombstone is critical: without it, a removed slot becomes a false terminator for any subsequent probe sequence that passed through it.
package main
import "fmt"
// Go has no built-in set. Use map[T]struct{}, struct{} occupies zero bytes.
type Set[T comparable] map[T]struct{}
func NewSet[T comparable]() Set[T] {
return make(Set[T])
}
func (s Set[T]) Add(value T) {
s[value] = struct{}{}
}
func (s Set[T]) Contains(value T) bool {
_, exists := s[value]
return exists
}
func (s Set[T]) Remove(value T) {
delete(s, value)
}
func (s Set[T]) Len() int {
return len(s)
}
func main() {
s := NewSet[int]()
s.Add(1)
s.Add(2)
s.Add(2)
fmt.Println(s.Contains(2)) // true
fmt.Println(s.Contains(3)) // false
s.Remove(2)
// Inline idiom, idiomatic Go skips the wrapper
seen := make(map[int]struct{})
seen[42] = struct{}{}
if _, ok := seen[42]; ok {
fmt.Println("seen 42")
}
}Go has no built-in set type. Go's answer to this is to make you type map[T]struct{} until you appreciate what you had. The empty struct struct{}{} occupies zero bytes, so you pay for the map's bucket overhead and the key, never a dummy value. Go's map uses a bucketed hash table with 8 key-value pairs per bucket and overflow buckets for spillover, backed by AES hardware instructions on capable CPUs when the key type allows it.
use std::collections::HashSet;
fn main() {
let mut s: HashSet<i32> = HashSet::new();
s.insert(1);
s.insert(2);
s.insert(2); // returns false, no-op
println!("{}", s.contains(&2)); // true
s.remove(&2);
// Set operations, return iterators, collect into a new HashSet
let a: HashSet<i32> = [1, 2, 3].iter().cloned().collect();
let b: HashSet<i32> = [2, 3, 4].iter().cloned().collect();
let union: HashSet<_> = a.union(&b).cloned().collect();
let intersection: HashSet<_> = a.intersection(&b).cloned().collect();
let difference: HashSet<_> = a.difference(&b).cloned().collect();
// Dedup a vec
let nums = vec![1, 2, 2, 3, 3, 3];
let unique: HashSet<i32> = nums.into_iter().collect();
// unique == {1, 2, 3}, order unspecified
}Rust's HashSet<T> is built on hashbrown, which uses Swiss table design (Robin Hood hashing, quadratic probing, SIMD-accelerated group lookups). The default hasher is SipHash 1-3, resistant to hash flooding. For trusted inputs where you need maximum throughput, replace the hasher with AHasher (ahash crate) or FxHasher (rustc-hash crate). BTreeSet<T> provides sorted iteration at O(log n) per operation.
import Foundation
var s: Set<Int> = []
s.insert(1)
s.insert(2)
s.insert(2) // returns (inserted: false, memberAfterInsert: 2)
print(s.contains(2)) // true
s.remove(2)
// Set literals and native operations
let a: Set<Int> = [1, 2, 3]
let b: Set<Int> = [2, 3, 4]
let union = a.union(b) // {1, 2, 3, 4}
let intersection = a.intersection(b) // {2, 3}
let difference = a.subtracting(b) // {1}
let symmetric = a.symmetricDifference(b) // {1, 4}
// Dedup an array preserving insertion order
func deduplicated<T: Hashable>(_ array: [T]) -> [T] {
var seen = Set<T>()
return array.filter { seen.insert($0).inserted }
}Swift's Set requires elements to conform to Hashable. The standard library provides Hashable conformance for all primitive types and tuples of Hashable types. Custom types implement hash(into:) on the Hasher type. Swift uses a hash randomization seed per process, similar to Python's PYTHONHASHSEED.
// Unordered hash set, O(1) average
val s = HashSet<Int>()
s.add(1)
s.add(2)
s.add(2) // returns false, element already present
println(s.contains(2)) // true
println(2 in s) // same thing, operator syntax
s.remove(2)
// Idiomatic Kotlin: setOf (immutable), mutableSetOf (mutable)
val a: Set<Int> = setOf(1, 2, 3)
val b: MutableSet<Int> = mutableSetOf(2, 3, 4)
val union = a union b
val intersection = a intersect b
val difference = a subtract b
// LinkedHashSet preserves insertion order
val ordered = linkedSetOf(3, 1, 2) // iterates: 3, 1, 2
// sortedSetOf wraps a TreeSet
val sorted = sortedSetOf(3, 1, 2) // iterates: 1, 2, 3Kotlin's HashSet<T> is a type alias for Java's java.util.HashSet on the JVM target. On Kotlin/Native and Kotlin/JS, the runtime provides platform-specific implementations. The setOf() factory returns an immutable view; the underlying type depends on element count (a singleton set has a different internal type than a multi-element set).
using System.Collections.Generic;
var s = new HashSet<int>();
s.Add(1);
s.Add(2);
s.Add(2); // returns false, no-op
Console.WriteLine(s.Contains(2)); // True
s.Remove(2);
// Collection initializer syntax
var a = new HashSet<int> { 1, 2, 3 };
var b = new HashSet<int> { 2, 3, 4 };
// Set operations, methods mutate the receiver
var unionSet = new HashSet<int>(a);
unionSet.UnionWith(b); // {1, 2, 3, 4}
var intersectSet = new HashSet<int>(a);
intersectSet.IntersectWith(b); // {2, 3}
var diffSet = new HashSet<int>(a);
diffSet.ExceptWith(b); // {1}
// Sorted iteration, wraps a red-black tree
var sorted = new SortedSet<int> { 3, 1, 2 }; // iterates: 1, 2, 3C#'s HashSet<T> uses open addressing internally (unlike Java's chaining approach). It requires T to implement GetHashCode() and Equals(). SortedSet<T> provides sorted iteration using a red-black tree at O(log n) per operation, analogous to TreeSet in Java.
require 'set'
s = Set.new
s.add(1)
s.add(2)
s.add(2) # no-op, returns the set
puts s.include?(2) # true
s.delete(2)
a = Set.new([1, 2, 3])
b = Set.new([2, 3, 4])
union = a | b # #<Set: {1, 2, 3, 4}>
intersection = a & b # #<Set: {2, 3}>
difference = a - b # #<Set: {1}>
symmetric = a ^ b # #<Set: {1, 4}>
# Dedup an array preserving order
arr = [1, 2, 2, 3, 3, 3]
deduped = arr.uniq # [1, 2, 3]Ruby's Set is implemented in pure Ruby on top of a Hash, with dummy true values. require 'set' is needed in Ruby versions before 3.2; from Ruby 3.2 onward, Set is auto-loaded. For performance-critical membership checks, using a plain Hash with key? is marginally faster since it skips the Set wrapper.
<?php
// PHP has no built-in set type. Use array keys, they map to a hash table.
$s = [];
$s[1] = true;
$s[2] = true;
$s[2] = true; // overwrites same key, still one entry
var_dump(isset($s[2])); // bool(true)
unset($s[2]);
// Build a set from an array
$a = array_fill_keys([1, 2, 3], true);
$b = array_fill_keys([2, 3, 4], true);
$union = $a + $b; // {1, 2, 3, 4}
$intersection = array_intersect_key($a, $b); // {2, 3}
$difference = array_diff_key($a, $b); // {1}
// Dedup an array
$arr = [1, 2, 2, 3, 3, 3];
$deduped = array_values(array_unique($arr)); // [1, 2, 3]
// O(1) membership check
function contains(array $set, $value): bool {
return isset($set[$value]);
}PHP has no dedicated set type, which is on brand. The idiomatic approach uses PHP arrays as hash tables, where array keys function as set members and values are ignored (using true as a cheap placeholder). isset($arr[$key]) is O(1). The Zend engine backs PHP arrays with a hash table using a packed arData structure: hash values stored as 32-bit bucket offsets, with keys and values stored in insertion order. array_unique() preserves original keys; wrap with array_values() for a clean integer-indexed result.
When to Reach for a Hash Set
Membership testing at scale. "Is user X in the blocked list?" Sorting and binary searching costs O(log n) per query after O(n log n) preprocessing. The set gives O(1) with no preprocessing per query.
Deduplication. Feed in a stream, add to a set, read out the set. Duplicates collapse automatically. Order is lost unless you use an insertion-ordered variant.
The seen-before pattern. You are iterating and need to know, at each step, whether you have encountered a value before. This is the most common interview use case. The set builds a running memory of what you have already visited.
Complement checking. "Does the complement of this element exist in what I've seen so far?" Two Sum lives here. You want target - current. The set answers in O(1) whether it exists among previously processed elements.
Set algebra on collections. Union, intersection, difference, symmetric difference. These have names because they're common enough to deserve them, and hash sets make them efficient.
Recognizing the Interview Signal
The signal is usually one of these questions buried inside the problem:
- Have I seen this value before?
- Is this a duplicate?
- Does the complement/pair of this element exist?
- Is this element a member of some constraint collection?
If you catch yourself writing "for each element, scan everything else," stop. That's O(n²). The set fixes it. Go find one.
Worked Example 1: Two Sum
Problem. Given an array of integers and a target sum, return the indices of two numbers that add up to the target.
Naive approach. For each element, scan all previous elements for the complement. O(n²).
The hash set insight. For each number x, the complement is target - x. Instead of scanning back through everything you've seen, ask: "have I already seen target - x?" One O(1) lookup instead of an O(n) scan. That's the whole trick.
def two_sum(nums: list[int], target: int) -> list[int]:
seen: dict[int, int] = {} # value -> index (need the index too, so a map)
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return []We use a map here because we need the index, not just presence. The mental model is still the seen-before check. The map is a set with a value riding shotgun. Knowing when to reach past a set to a map is exactly what interviewers are testing. A lot of candidates reach for the map automatically, never stopping to ask why.
Worked Example 2: Longest Consecutive Sequence
Problem. Given an unsorted array of integers, find the length of the longest consecutive sequence. Required time complexity: O(n). (LeetCode 128.)
Naive approach. Sort, then scan for runs. O(n log n).
The hash set insight. We need O(1) membership checks, and we need to avoid re-processing numbers. Build a set of all values. For each number, start counting only if num - 1 is not in the set. That means num is the start of a sequence. Then walk forward until the sequence breaks.
def longest_consecutive(nums: list[int]) -> int:
num_set = set(nums)
best = 0
for num in num_set:
if num - 1 not in num_set: # only start from sequence beginnings
current = num
length = 1
while current + 1 in num_set:
current += 1
length += 1
best = max(best, length)
return bestWhy this is O(n). Each element is visited at most twice: once when considered as a potential sequence start, and once as a continuation of some sequence. The num - 1 not in num_set guard ensures the inner while loop fires only from actual sequence starts. Every in check is O(1). Without the set, current + 1 in nums is O(n) per check and the whole algorithm blows up to O(n²). The set is not a nice-to-have here. Without it, O(n) is not achievable. That's the whole point of this pattern.
Where Engineers Go Wrong
Mutating elements after insertion. The element changed its hash. The set still thinks it's in the old bucket. You now have a ghost: it exists but cannot be found, and it will not show up as a duplicate either. In Python, only immutable types (int, str, tuple) are hashable by default. Adding a custom object and then changing the fields that affect its hash corrupts the set silently.
Expecting iteration order. Most hash sets give no ordering guarantees. If you need insertion-order iteration, use dict in Python 3.7+, LinkedHashSet in Java, or linkedSetOf() in Kotlin. If you need sorted iteration, use SortedSet / TreeSet.
Forgetting to implement hash and equality for custom types. In Java, the contract is: if a.equals(b) then a.hashCode() == b.hashCode(). Override one without the other and you have a set that happily stores "duplicates" and lies about it when you ask. Override neither and enjoy a set that treats every object as unique regardless of content. The compiler will not warn you.
Assuming O(1) is always faster. A TreeSet (O(log n)) can outperform a HashSet (O(1) average) for small n, because the tree has better cache behavior and no hash computation. For n under a few hundred, measure before committing.
Quick Recap
- A hash set stores unique elements in a hash table, providing set semantics: membership, not association.
- Average O(1) for add, remove, and contains. Worst case O(n) under adversarial hash collisions.
- Resizing is O(n) but triggers at most O(log n) times across n insertions. The amortized cost per operation stays O(1).
- CPython uses open addressing with a hybrid probe sequence; Java wraps HashMap; C++ uses separate chaining; Rust uses Swiss table design with SipHash; Go uses 8-slot buckets with overflow chains.
- Hash flooding is a real attack. Python 3.3+, Rust, and Java 8+ each have mitigations.
- Reach for a hash set when the question is "have I seen this?" or "does this element exist?"
- Two Sum (complement check) and Longest Consecutive Sequence (sequence start detection) are the canonical interview patterns.
Recognizing the hash set signal under time pressure is a skill, not just knowledge. SpaceComplexity runs you through live mock interviews where you have to hear a problem, spot the pattern, explain your reasoning out loud, and code it, which is a harder test than recognizing it in a blog post.
For two-pointer and sliding window problems that frequently pair with hash sets for the "seen-before" window check, see Nested Loops Will Cost You the Offer. The Sliding Window Algorithm Won't.. And if you're reaching for a set as the memoization container in a dynamic programming problem, the framework in Dynamic Programming Is Just Recursion With a Memory applies directly.
