Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Data Structures  ›  Lesson

Map and Set

Map 12 minA Map stores key-value pairs with keys of any type in insertion order, and a Set stores unique values.

Map and Set

In short: a Map pairs keys with values, like an object, but any value can be a key and keys are never converted to strings. A Set keeps each value exactly once — adding a duplicate does nothing. Both remember insertion order and use .size, not .length, plus their own methods (get/set/has for Map; add/has for Set) rather than square brackets. Use a Map when you need to look something up by key; use a Set when you only need to know "is it in here?" with no duplicates.

A Map box with labelled jars: "Blue Hour" → 3, "Red Dawn" → 1, any value → any value, remembers insertion order, uses .size not .length; next to a Set jar holding unique marbles Blue/Red/Green, with a faded second "Blue" marble ignored because it's already in the jar

Count plays with a Map

A Map's get/set/has work by key, and the key can be anything — here it's a song title:

javascript

get on a missing key returns undefined rather than throwing, so a counting pattern usually reads the current count first and falls back to a starting value:

javascript

This prints [["Blue Hour", 2], ["Red Dawn", 1]] — Blue Hour played twice, in the order each title was first seen.

Counting plays with a MapStep 1 of 4
  1. A song finishes

    "Blue Hour" ends. Read its current count: plays.get("Blue Hour") is 3.

Keep a liked-songs list with no duplicates

A Set stores values, not pairs, and each value can appear only once:

javascript

The second add is silently ignored — size is still 1. The most common Set trick removes duplicates from an array while keeping first-seen order:

javascript

This prints ["Blue Hour", "Red Dawn"] — the repeated "Blue Hour" at the end collapses away.

The trap: a number key and its string look-alike

Object keys are always converted to strings, but Map keys are not — a number key and a string that looks the same are two different keys:

javascript

This prints undefined, not "one" — 1 (the number) and "1" (the string) are separate keys in a Map, exactly the distinction that Data Types warned about. Treating a Map like a plain object, m["Blue Hour"], does not read its entries at all; always use get and set.

Quick practice

quick drill

What is new Set(["a", "b", "a"]).size?

quick drill

After const m = new Map(); m.set("Blue Hour", 3);, what is m.get("Blue Hour")?

quick drill

After const m = new Map(); m.set(1, "one");, what is m.get("1")?

quick drill

What is [...new Set([3, 1, 3, 2, 1])]?

quick drill

You need to know whether a listener has already liked a song, with no duplicates. Which fits best?

🎯 Your turn

For a listening history ["Blue Hour", "Red Dawn", "Blue Hour"], explain how you would get the list of distinct songs and how you would count plays per song, naming the collection for each and why.

Write your explanation and submit it — there's no code to run here, just your reasoning.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainChoose the collection

For a listening history ["Blue Hour", "Red Dawn", "Blue Hour"], explain how you would get the list of distinct songs and how you would count plays per song, naming the collection for each and why.