Sets, deduplication and set algebra
Membership without values, and the operations that make intersections cheap.
A set is a hash map that stores keys and no values. Use one whenever the question is "have I seen this?" rather than "what is it worth?".
const seen = new Set();
for (const item of items) {
if (seen.has(item)) return true; // O(1) — this is duplicate detection
seen.add(item);
}
That replaces the O(n²) nested loop from the very first chapter with an O(n) pass.
Deduplication
const unique = [...new Set(items)]; // O(n), insertion order preserved
Compare with the includes-in-a-loop version, which is O(n²) and looks equally innocent. This is the single most common accidental quadratic in JavaScript.
Set algebra
const a = new Set([1, 2, 3]);
const 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]
Each is O(n + m) because has is O(1). Doing the same with arrays and includes is O(n × m) — the difference between milliseconds and minutes at scale.
For efficiency, always iterate the smaller set and test against the larger.
What a set cannot do
Sets store keys by value identity: objects compare by reference, so two structurally identical objects are two members.
new Set([{ id: 1 }, { id: 1 }]).size; // 2, not 1
To deduplicate objects, key on something primitive — an id, or a canonical string — and keep a Map from that key to the object.
Sets versus sorting
Deduplicating by sorting first is O(n log n) and gives you order for free. Deduplicating with a set is O(n) and preserves insertion order. If you need sorted output anyway, sorting costs nothing extra; if you do not, the set wins.
Beyond memory
When the set no longer fits in memory, the tools change but the question does not: Bloom filters answer membership probabilistically in a few bits per element, and HyperLogLog counts distinct values in kilobytes regardless of cardinality. Both trade exactness for space, and both are worth naming when an interviewer says "now do it for a billion items".