The guest list for a wedding is put together from three WhatsApp groups, and Rina's name appears three times. She's still one guest. What you need here is a collection that refuses duplicate names by itself.
That's what a Set is for: a collection where each value can appear only once. If the guest list were a Set, each name would be one value, and Rina would be on it only once. A value that's already there and gets added again is ignored, so the values inside are always unique (no two are the same). A Set can also answer "is this value in here?" very quickly.
A Set: every value only once
This code makes a Set of four numbers, then adds, checks and removes values:
const numbers = new Set([1, 3, 5, 7]);
numbers.add(9);
numbers.add(3); // already there, ignored
console.log(numbers.size); // 5
console.log(numbers.has(5)); // true
console.log(numbers.has(4)); // false
numbers.delete(1);
console.log([...numbers]); // [3, 5, 7, 9]
new Set([1, 3, 5, 7])makes a Set from an array. The values go inside one array, notnew Set(1, 3, 5, 7).add(9)adds one value.add(3)changes nothing, because 3 is already there.has(5)answerstrueorfalse.delete(1)removes one value.sizeis the number of values. A Set has nolength.[...numbers]turns the Set into an array. The three dots (spread) take the Set's values out one by one and put them into a new array.
A Set keeps insertion order: the values stay in the order in which they were first added.
Removing duplicates from an array
Before you read the comments in this code, guess: how many values does new Set(withDuplicates) hold, and in what order?
const withDuplicates = [3, 1, 3, 5, 1, 7, 3];
const unique = [...new Set(withDuplicates)];
console.log(unique); // [3, 1, 5, 7]
console.log(withDuplicates); // [3, 1, 3, 5, 1, 7, 3]
When new Set(withDuplicates) is made, the values of withDuplicates go in one by one, from the front:
| Step | Value | Already there? | What the Set holds |
|---|---|---|---|
| 1 | 3 | no, goes in | 3 |
| 2 | 1 | no, goes in | 3, 1 |
| 3 | 3 | yes, ignored | 3, 1 |
| 4 | 5 | no, goes in | 3, 1, 5 |
| 5 | 1 | yes, ignored | 3, 1, 5 |
| 6 | 7 then 3 | 7 goes in, 3 ignored | 3, 1, 5, 7 |
The result is 4 values, in the order they first appeared: 3, 1, 5, 7. Then [...] turns the Set into an array. The array withDuplicates doesn't change. In the exercise, the function receives an array arr, so return [...new Set(arr)]; is all it takes. Array.from(new Set(arr)) gives the same result.
has is faster than includes
arr.includes(x) checks whether x is in the array by looking at its items one at a time, from the front. That's O(n): an array twice as long takes roughly twice as long. set.has(x) works the same way as the hash map in the last lesson: a hash function turns x into a bucket number, and only that bucket is opened. So has is O(1) on average: one step, however big the Set is.
| Array | Set | |
|---|---|---|
| Repeated values | allowed | thrown out automatically |
Get by position (arr[0]) |
yes | no |
| "Is it there?" | includes, O(n) |
has, O(1) on average |
So use a Set when you need unique values and often check "is this already there?", like a list of pages already visited. Use an array when the position of a value matters (like arr[0]), or when values may repeat.
⚠️ When a Set gets mixed up with an array
- Returning a Set when an array was asked for.
return new Set(arr)returns a Set, and a Set has nojoinormap(two methods of arrays). Turn it into an array first:[...new Set(arr)]. lengthon a Set. It givesundefined. A Set's count is insize.- Values written without an array.
new Set(1, 2, 3)causes an error, because a Set needs one array (or another list) that holds its values:new Set([1, 2, 3]).
📌 Union, intersection and difference
This code combines and compares two Sets, a and b:
const a = new Set([1, 2, 3, 4]);
const b = new Set([3, 4, 5, 6]);
const union = new Set([...a, ...b]);
const intersection = [...a].filter((x) => b.has(x));
const difference = [...a].filter((x) => !b.has(x));
console.log([...union]); // [1, 2, 3, 4, 5, 6]
console.log(intersection); // [3, 4]
console.log(difference); // [1, 2]
The union, stored in union, is every value from both Sets. The intersection, in intersection, is the values that are in both Sets. The difference, in difference, is the values that are in a but not in b. A Set has no filter, so [...a] turns a into an array first.
Now there is a shorter way: built-in Set methods, which every major browser has had since June 2024, and Node.js 22 and later too. With the same a and b:
console.log([...a.union(b)]); // [1, 2, 3, 4, 5, 6]
console.log([...a.intersection(b)]); // [3, 4]
console.log([...a.difference(b)]); // [1, 2]
console.log(new Set([3, 4]).isSubsetOf(a)); // true
The first three methods give a Set, so they are printed with [...]. isSubsetOf answers whether every value of one Set is in another Set. Use these built-in methods when your program runs in a recent browser or Node.js. The spread and filter way above is still useful for older browsers.
TL;DR: A Set is a collection of unique values: each value can be there only once, so add with a value that's already there is ignored. You make a Set from an array with new Set([...]), and size counts what's inside. has checks whether a value is there in O(1) on average (the steps stay the same), faster than includes on an array, which looks at the items one at a time, O(n). A Set keeps the order values first appeared in. To remove duplicates (repeated values) from an array arr: [...new Set(arr)], which returns a new array and leaves the original alone. Use a Set for unique values and fast "is it there?" checks. Use an array when the position of a value matters or values may repeat.
⚡ Every value only once
Write:
const numbers = new Set([1, 3, 5, 7]);
numbers.add(9);
numbers.add(3); // already there, ignored
console.log(numbers.size); // 5
console.log(numbers.has(5)); // true
Meaning: new Set([1, 3, 5, 7]) makes the Set numbers, a collection where each value can be there only once. add adds a value, but a value that's already there is ignored: add(3) changes nothing. has answers whether a value is there. The count is in size (5 here), not length.
⚡ Remove duplicates from an array
Write:
const withDuplicates = [3, 1, 3, 5, 1, 7, 3];
const unique = [...new Set(withDuplicates)];
Guess first: new Set(withDuplicates) removes the repeated values, then [...] turns that Set into an array. What does unique hold, and in what order?
[3, 1, 5, 7]. A Set keeps the order values first appeared in. The array withDuplicates doesn't change.
Meaning: If your function has to return an array, don't return the Set itself. Turn it into an array first with [...], as this code does. A Set has no join or map, two methods of arrays.
⚡ has is faster than includes
Meaning: arr.includes(x) checks whether x is in the array arr by looking at its items one at a time, so it's O(n). set.has(x) works like a hash map, so it's O(1) on average: the steps stay the same, however big the Set is. Use a Set for unique values and fast "is it already there?" checks. Use an array when the position of a value matters or values may repeat.