Workshop › Text detective › Step 7 of 13
Which words repeat? A Map keeps a count for each word: we go through the words and add one to that word's count.
```js
const counts = new Map()
for (const word of words) {
const key = word.toLowerCase()
counts.set(key, (counts.get(key) ?? 0) + 1)
}
+ '\nDifferent words: ' + counts.size
```new Map() is an empty word → count table.for loop runs once per word. toLowerCase() makes The and the the same key.counts.get(key) ?? 0 is the count so far (0 if the word is new); set stores it plus one.counts.size is how many different words there are.words line write the counts map and the for loop.+ '\nDifferent words: ' + counts.size at the end of the report line.Skills: Loops, Arrays and objects
← An empty box has no words Drop the punctuation →