Glossary

Set

A set is an unordered collection of unique values, backed by a hash table. It provides O(1) average-case add, delete, and has (membership test) operations.

Explanation

A set's defining property is uniqueness: adding the same value twice leaves the set unchanged. Internally, sets are implemented as hash tables where the key and value are the same element — you're essentially storing a hash map with keys only and no values. This gives sets the same O(1) average-case performance for add, delete, and has that hash tables have for their operations. JavaScript's Set object stores values in insertion order and supports any value type (primitives or objects, though object equality is by reference). Python's set is unordered and doesn't guarantee insertion order. Both support the core set operations: union (all elements from both sets), intersection (elements in both sets), and difference (elements in one set but not the other). The most common use case for sets is membership testing — "have I seen this value before?" — which O(1) sets handle far better than O(n) array includes. De-duplication is the second most common use: new Set(array) creates a set of unique values; spreading it back [...new Set(array)] gives you a de-duplicated array. Set operations (union, intersection, difference) are powerful primitives for working with collections of IDs, tags, permissions, or any scenario where you care about set membership across groups. For example, finding mutual friends is an intersection of two sets of friend IDs.

Code Example

javascript
// JavaScript Set fundamentals
const seen = new Set();
seen.add('a'); seen.add('b'); seen.add('a'); // duplicate ignored
console.log(seen.size);       // 2
console.log(seen.has('a'));   // true  (O(1))
console.log(seen.has('c'));   // false (O(1))

// De-duplication
const withDups = [1, 2, 2, 3, 3, 3, 4];
const unique = [...new Set(withDups)]; // [1, 2, 3, 4]

// Set operations (not built-in, but trivial to write)
const a = new Set([1, 2, 3, 4]);
const b = new Set([3, 4, 5, 6]);

const union        = new Set([...a, ...b]);       // {1,2,3,4,5,6}
const intersection = new Set([...a].filter(x => b.has(x))); // {3,4}
const difference   = new Set([...a].filter(x => !b.has(x))); // {1,2}

console.log([...intersection]); // [3, 4]

Why It Matters for Engineers

Replacing an array with a set for membership testing is one of the highest-impact, lowest-effort performance improvements you'll make as an engineer. Changing array.includes(x) (O(n)) to set.has(x) (O(1)) can turn an O(n²) algorithm into O(n) — a dramatic speedup for large inputs. Understanding sets also makes graph algorithms cleaner: DFS and BFS both need a visited tracker, and a Set is the right tool (not an array, not an object with boolean values). It's also the right abstraction for tracking unique visitors, active sessions, feature flag recipients, and any scenario where you need "is this item in the group?" answered quickly.

Related Terms

Hash Table · Map · Array

Learn This In Practice

Go deeper with the full module on Beyond Vibe Code.

Data Structures Fundamentals → →