JavaScript · Level 2 Data

Map & Set

Drop duplicates with a Set and keep counts in a Map, then turn the results back into ordinary arrays.

  • A Set keeps each value once
  • A Map stores key-value pairs with get and set
  • [...set] turns a Set back into an array

A Set keeps each value once; a Map stores key-value pairs. Trap: a Set is not an array — it has no map or sort, so spread it back with [...set].

const seen = new Set(["a", "b", "a"]);
seen.size;                 // 2
[...seen];                 // ["a", "b"]

const counts = new Map();
for (const w of ["hi", "yo", "hi"]) counts.set(w, (counts.get(w) ?? 0) + 1);
counts.get("hi");          // 2

Write uniqueTags(posts) that returns every tag from the posts' tags arrays once, sorted A to Z, for example [{ tags: ["js", "css"] }, { tags: ["css"] }] gives ["css", "js"].

solution.js
Test Suites Pending
Output