A Set is a collection where every value appears only once. Try to add a duplicate and the Set simply ignores it. Whenever the question is "which different values are there?" or "have I seen this before?", a Set is the right tool.
const empty = new Set();
const colors = new Set(["red", "green", "red", "blue", "green"]);
console.log(colors);
console.log(colors.size);
Five values went in, but only three unique ones were kept. Notice it is size, not length.
A Set can hold values of any type: strings, numbers, booleans, null, objects, even arrays. When you add a variable, the Set stores the variable's value:
const a = "a";
const letters = new Set();
letters.add(a);
letters.add("a"); // the same value: ignored
console.log(letters.size);
const tags = new Set();
tags.add("js");
tags.add("css");
tags.add("js"); // already there: ignored
console.log(tags.has("css")); // true
console.log(tags.has("php")); // false
tags.delete("css");
console.log(tags.size); // 1
Turning an array into a Set and back again is the shortest way to remove duplicates:
const votes = ["pizza", "tacos", "pizza", "sushi", "tacos", "pizza"];
const choices = [...new Set(votes)];
console.log(choices);
You could check an array with includes before every push. But includes has to scan the array from the start each time, which gets slow as the list grows. has on a Set answers almost instantly, no matter how big the Set is.
// Array: slower as it grows, and duplicates are your job
if (!seenList.includes(id)) seenList.push(id);
// Set: fast, and duplicates are impossible
seenSet.add(id);
const langs = new Set(["JS", "Python", "Rust"]);
for (const lang of langs) {
console.log(lang);
}
langs[0] is undefined. If you need positions, convert to an array first: [...langs][0].
Primitives are compared by value. Objects are compared by identity, just like ===:
const s = new Set();
s.add(5);
s.add("5"); // different type: both kept
s.add({ id: 1 });
s.add({ id: 1 }); // a different object: both kept
s.add(NaN);
s.add(NaN); // Sets treat NaN as equal to itself: one kept
console.log(s.size);
const letters = new Set(["a", "b"]);
console.log(typeof letters);
console.log(letters instanceof Set);
Like arrays, typeof just says "object". Use instanceof Set to check for a Set. Also, size is read-only: letters.size = 10 is silently ignored. The only way to change the size is to add or delete values.
const visits = ["ava", "ben", "ava", "cal", "ben", "ava"];
const unique = new Set(visits);
console.log(`${visits.length} visits from ${unique.size} people`);
What does this log?
const s = new Set(["a", "b", "a"]);
s.add("b");
console.log(s.size);
2. The Set starts with "a" and "b" (the second "a" is dropped), and adding "b" again changes nothing.How do you get the number of values in a Set s?
s.lengths.sizes.count()s.length()size, and it is a property, not a method (no parentheses). s.length is undefined.What does this log?
const s = new Set([10, 20, 30]);
console.log(s[0], [...s][0]);
undefined 10. Sets have no index positions. Spread the Set into an array first when you need one.nums itself too[...new Set("mississippi")]