ProgramGuru

Map and Set Programs

Map and Set Programs — purpose and prerequisites

Map stores keyed entries and Set stores unique values. Both retain insertion order when iterated. Use Map when keys are not naturally object property strings or when a purpose-built collection API is useful.

What you’ll learn

  • Explain the purpose of map and set programs.
  • Identify the typed contract, runtime responsibilities and common pitfalls.
  • Run the example and extend its behavior with a practical exercise.

How the idea works

Map.get returns a value or undefined, so the nullish fallback starts the count at zero. Set removes duplicate names and preserves the order in which unique items first appeared.

Do not use map[key] to read a Map entry; use map.get(key).

Visualize the flow

Map and Set Programs — learning flowInput entries → Map counts / Set uniqueness → Collection outputMap and Set Programs1Input entries2Map counts / Set uniqueness3Collection outputRun the example and test a boundary case.
A three-step view of the responsibilities and the observable result.

Run the checked TypeScript example

This source was compiled using TypeScript 5.8.3 with strict checking, then executed with Node.js 22.16.0. Its stdout below is the actual captured output. The core logic is runnable without a framework or a remote API; integration-specific examples appear in a separate lab when needed.

TypeScript source

const purchases = new Map<string, number>([["apple", 2], ["banana", 1]]);
purchases.set("apple", (purchases.get("apple") ?? 0) + 3);
const unique = new Set(["Maya", "Liam", "Maya"]);
console.log([...purchases.entries()]);
console.log([...unique.values()]);

Verified output

[ [ 'apple', 5 ], [ 'banana', 1 ] ]
[ 'Maya', 'Liam' ]

Compile: npx tsc --strict --target es2022 --module commonjs map-set-programs.ts; run: node map-set-programs.js. For examples importing node: modules, install @types/node as a development dependency; for browser or React projects use the separate lab setup.

Trace the execution

Map.get returns a value or undefined, so the nullish fallback starts the count at zero. Set removes duplicate names and preserves the order in which unique items first appeared.

Before editing the code, predict what the output will be for one changed input. Run it and compare your prediction with the actual result.

Common mistake and correction

Do not use map[key] to read a Map entry; use map.get(key).

Determine whether the issue belongs to the type system, a trust boundary, or actual runtime behavior before choosing a correction.

Your turn: extend the example

Build a word-frequency Map from a sentence and return its entries sorted by descending count.

Record expected success and failure outcomes, run the code, and compare the observed behavior. Keep the exercise compatible with strict type checking.

Check your understanding

Which statement correctly describes map and set programs in the example?
Choose one answer. You can try again.

Answer and explanation

Map.

Map.get returns a value or undefined, so the nullish fallback starts the count at zero. Set removes duplicate names and preserves the order in which unique items first appeared.

Key idea to remember

Map.get returns a value or undefined, so the nullish fallback starts the count at zero. Set removes duplicate names and preserves the order in which unique items first appeared.

Apply the same reasoning to the practice task, then integrate it into a real app only when the runtime and dependency setup are ready.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Function Programs →