Concepts
Frequency map
Count occurrences with a key-to-count map. Use a set for membership; define key equality and distinguish expected hash-table costs from ECMAScript guarantees.
Formal Syntax & Structure
javascript syntaxjavascript
counts.set(key, (counts.get(key) ?? 0) + 1)Working Example
Example Snippetjavascript
const counts=new Map();for(const label of ["pen","box","pen","pen"])counts.set(label,(counts.get(label)??0)+1);console.log(JSON.stringify([...counts]));