Set Operations (add, delete, has)

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

▶ Do the exercises

Exercise 1 — Adding Values to a Set

Imagine updating your Codédex as you explore: every time you catch a Codémon, you want to register its name—without ever creating duplicates if you catch the same species twice. To insert a value into a Set, use the `.add(value)` method. If the value is already inside the Set, `.add()` simply does nothing:

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

Even though `.add()` was called `3` times, `codedex` only contains `2` entries (`"Zap"` and `"Aquachu"`). You never need to write an `if` check before adding an item—the Set prevents duplicates for you!

Exercise 2 — Checking If a Value Exists

To check whether a value is inside a Set, use the `.has(value)` method. Just like `.includes()` on an array, it returns `true` if the value is found, or `false` if it isn't:

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

Because `.has()` returns a boolean, you can use it directly inside an `if` statement:

const codedex = new Set(["Zap", "Aquachu"]); if (codedex.has("Pyrochu")) { console.log("Already in the Codédex"); } else { console.log("New Codémon discovered!"); }

Why use Set.has() instead of Array.includes()?

An array's `.includes()` has to check items one by one from the beginning. A Set's `.has()` looks up the value instantly, even if the Set holds thousands of items!

Exercise 3 — Removing and Clearing Values

To remove a single value from a Set, use `.delete(value)`. It removes the item and returns `true` if the value was in the Set, or `false` if it wasn't there:

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

And if you want to remove every item at once and reset the Set back to `0` items, call `.clear()`:

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

Exercise 4 — Quiz: After the Adds

What does this code print?

const s = new Set(); s.add("a"); s.add("b"); s.add("a"); console.log(s.size);
Options
• 2
• 3
• 1
• 0

Hint : Adding a value that is already there changes nothing.

Exercise 5 — Quiz: Is It There?

What does this code print?

const s = new Set(["Zap"]); console.log(s.has("Metachu"));
Options
• false
• true
• undefined
• -1

Hint : This method answers with one of two values only.

Exercise 6 — Catch a New One

Record Metachu in the Codédex, then check it is there.

Hint : A Set uses one method to insert a value and another to ask a yes-or-no question about it.

Exercise 7 — Sets Are Modified in Place

Unlike string methods or `.map()` and `.filter()`, which create a brand-new copy, `.add()`, `.delete()`, and `.clear()` modify the Set directly in place. Just like with arrays and objects, if two variables point to the same Set, any change made through one variable is visible through the other:

const codedex = new Set(["Zap"]); const backup = codedex; backup.add("Aquachu"); console.log(codedex.size);

To create an independent copy of a Set, pass the original Set into `new Set(originalSet)`. Now modifying the copy leaves the original untouched:

const codedex = new Set(["Zap"]); const copy = new Set(codedex); copy.add("Aquachu"); console.log(codedex.size); console.log(copy.size);

Exercise 8 — Release a Codémon

Remove Zap from the Codédex, then print how many are left.

Hint : One method takes a value out, and a Set counts its items its own way.

Exercise 9 — Set Methods

Match each method with what it does.

Items to match
• add(x)
• has(x)
• delete(x)
• clear()
Definitions
• Inserts x, ignores duplicates
• true or false
• Removes x
• Empties the whole Set

Exercise 10 — Live Codédex

Play a short walk through the tall grass, recording each encounter as it happens.

Step 1: start from an empty Set named `codedex`. Step 2: for each encounter, if the Codémon is already recorded print `"Already seen: X"`, otherwise add it and print `"New entry: X"`. Step 3: print `"Codédex: N"` with the final size. Step 4: Zap escapes and must be removed. Delete it, then print `"After escape: N"` and `"Still has Aquachu: X"`.

Expected output:

New entry: Zap New entry: Aquachu Already seen: Zap New entry: Metachu Codédex: 3 After escape: 2 Still has Aquachu: true
Starter code
const encounters = ["Zap", "Aquachu", "Zap", "Metachu"]; // Write your code here
Back to the course