Creating Sets

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — A Collection Without Duplicates

Imagine you record every Codémon you encounter in the tall grass inside an array. Because you run into the same species multiple times, your array fills up with duplicates—so checking `.length` doesn't tell you how many different Codémon you've discovered:

const encounters = ["Zap", "Aquachu", "Zap", "Pyrochu", "Aquachu"]; console.log(encounters.length);

There were `5` encounters, but only `3` unique Codémon (`"Zap"`, `"Aquachu"`, and `"Pyrochu"`). To keep only unique values, JavaScript gives you a Set. When you pass an array to `new Set(array)`, all duplicate values are automatically removed:

const encounters = ["Zap", "Aquachu", "Zap", "Pyrochu", "Aquachu"]; const codedex = new Set(encounters); console.log(codedex.size);

Notice that a Set uses `.size` (not `.length`) to tell you how many unique items it contains!

Exercise 2 — Turning a Set Back Into an Array

A Set is not an array: you cannot read items by index (like `codedex[0]`), and array methods like `.map()` or `.filter()` do not exist on it. Whenever you want a regular array without duplicates, use the spread operator (`...`) inside square brackets `[...mySet]` to unpack the Set back into an array:

const encounters = ["Zap", "Aquachu", "Zap", "Pyrochu"]; const unique = [...new Set(encounters)]; console.log(unique); console.log(unique.length);

Writing `[...new Set(array)]` is the classic one-line trick in JavaScript to remove duplicates from an array: 1. `new Set(encounters)` strips out the duplicates. 2. `[... ]` turns the unique values right back into a standard array. Bonus: a Set always preserves the original order in which each value first appeared!

Exercise 3 — Empty Sets and Other Types

You don't always have all your data ready from the start. Calling `new Set()` with no arguments creates an empty Set (`size` of `0`) that you can fill later as your game runs:

const codedex = new Set(); console.log(codedex.size);

A Set can also store any type of value—not just strings, but also numbers or booleans. Whatever the type, each unique value is stored only once:

const levels = new Set([4, 12, 4, 8, 12]); console.log(levels.size); console.log([...levels]);

Exercise 4 — Quiz: How Many Left?

What does this code print?

const s = new Set(["a", "b", "a", "c", "b"]); console.log(s.size);
Options
• 3
• 5
• 2
• 1

Hint : Count the different values, not the total number of items given.

Exercise 5 — Build the Codédex

Count how many different Codémon were met.

Hint : One collection refuses duplicates, and it doesn't count its items the way an array does.

Exercise 6 — How a Set Checks for Duplicates

To decide if two values are duplicates, a Set compares them strictly—just like the `===` operator: - Uppercase and lowercase strings are different (`"Zap"` vs. `"zap"`). - Numbers and strings are different (`5` vs. `"5"`).

const items = new Set(["Zap", "zap", 5, "5"]); console.log(items.size); console.log([...items]);

Watch out with objects

Two separate objects `{ name: "Zap" }` and `{ name: "Zap" }` are stored at different places in memory, so `{ name: "Zap" } === { name: "Zap" }` is `false`. Because of this, a Set treats them as two distinct items and keeps both! Sets work best with primitive values like strings and numbers.

const team = new Set([{ name: "Zap" }, { name: "Zap" }]); console.log(team.size);

Exercise 7 — Quiz: Different Types

What does this code print?

const s = new Set([1, "1", 1]); console.log(s.size);
Options
• 2
• 1
• 3
• 0

Hint : A number and a piece of text are never the same value.

Exercise 8 — Remove the Duplicates

Turn the array into a duplicate-free array.

Hint : One symbol pours a collection into an array, and one collection refuses repeats.

Exercise 9 — Set Cheat Sheet

Match each expression with its result.

Items to match
• new Set([1, 1, 2]).size
• new Set().size
• [...new Set(arr)]
• How a Set counts its items
Definitions
• 2
• 0
• An array without duplicates
• .size

Exercise 10 — Codédex Counter

After a long walk in the tall grass, count how much of the Codédex you actually filled.

Step 1: print `"Encounters: N"` with the total number of encounters. Step 2: build a Set from the encounters and print `"Different Codémon: N"` with its size. Step 3: turn that Set back into an array, sort it alphabetically, and print the names joined with `", "`. Step 4: print `"Repeats: N"` with the number of encounters that were duplicates.

Expected output:

Encounters: 7 Different Codémon: 4 Aquachu, Metachu, Pyrochu, Zap Repeats: 3
Starter code
const encounters = ["Zap", "Aquachu", "Zap", "Metachu", "Aquachu", "Pyrochu", "Zap"]; // Write your code here
Back to the course