JavaScript Map and Set
Map stores key-value associations without the prototype-key concerns of using a plain object as an arbitrary key dictionary. Set stores unique values.
JavaScript
const words = ["red", "blue", "red"];
const counts = new Map();
for (const word of words) {
counts.set(word, (counts.get(word) ?? 0) + 1);
}
console.log(JSON.stringify([...counts]));
console.log(new Set(words).size);
if (counts.has("green")) throw new Error("missing key");