Objects & collections
Set
A collection where every value is stored only once.
Why it exists
Programs constantly ask "have we seen this before?": is this email already invited, is this item already in the cart, has this song already played. With a plain array you'd scan the whole thing every time and weed out repeats by hand. A Set does both for you: it never stores a repeat, and has answers almost instantly even with a million values inside.
Where it's used
- Remove duplicates from a list: unique article tags, unique cities in orders —
[...new Set(list)]. - Remember what's already done: pages a web crawler has visited, notifications already shown.
- Fast "is it in the list?" checks: blocked users, selected filters in a shop.
- Finding duplicates — like in the "Any duplicates?" challenge.
Syntax
const set = new Set();
set.add(value);
set.has(value); // true or falseExamples
const tags = new Set();
tags.add("js");
tags.add("css");
tags.add("js"); // a repeat isn't added
tags.has("css"); // → true
tags.has("html"); // → false
tags.size; // → 2const cities = ["Baku", "Ganja", "Baku", "Sheki", "Ganja"];
[...new Set(cities)]; // → ["Baku", "Ganja", "Sheki"]const s = new Set([1, "1", NaN, NaN]);
s.size; // → 3 — 1 and "1" differ, NaN is kept once
const users = new Set();
users.add({ id: 1 });
users.has({ id: 1 }); // → false — that's a different objectSet decides two values are the sameResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
Inside, a Set works like a hash table: from the value itself it computes the "address" of the slot where it belongs. So has doesn't walk through the items one by one — it looks straight into the right slot, and the time barely depends on the size. An array's includes has to check items one after another.
Values are compared almost like ===: 1 and "1" are different, and two objects {} are different too even if they look the same, because references are compared, not contents. The one difference from ===: inside a Set, NaN equals NaN.
A Set remembers insertion order, so when you loop over it, values come out in the order they were added.
Traps
How it differs from…
- Property access with
[] An object can also remember "seen / not seen" (
seen[key] = true), but object keys are always turned into strings:1and"1"become the same key. ASetkeeps values as they are.
When to pick something else
If you need to store extra data with each value (how many times it appeared, when it was added), use a Map or an object. If positions matter or repeats are allowed (two identical items in a cart), use a plain array.
Practice
Lessons where this appears
- Byte's cat showTale · Chapter 11 · Map and Set
- Why not a plain object?Why? · Chapter 11 · Map and Set
- The front deskWorkshop · Chapter 11 · Map and Set
- The case of the look-alike crowdInvestigation · Chapter 11 · Map and Set
- Has duplicatesLab · Chapter 11 · Map and Set
- The DoppelgangerBoss · Chapter 11 · Map and Set