One Pager Cheat Sheet
- This challenge requires returning a list of duplicate words in a sentence that consists of ASCII characters with a time complexity of
O(n)and a space complexity ofO(n). - We can use a
hashmap(or JS object orPythondict) to store each word's frequency by looping through each one in the array and recording its occurrence. - We can easily find the duplicates by counting the occurrences of each word and looking for those with
2!
This is our final solution.
To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.
xxxxxxxxxx47
}var assert = require('assert');​function findDuplicates(str) { const dupes = []; const strLowerCase = str.toLowerCase(); const strArr = strLowerCase.split(' '); const wordFreqCounter = {};​ strArr.forEach((word) => { if (!wordFreqCounter[word]) { wordFreqCounter[word] = 1; } else { wordFreqCounter[word] += 1; } });​ let allKeys = Object.keys(wordFreqCounter);​ allKeys.forEach((key) => { if (wordFreqCounter[key] > 1) { dupes.push(key); } });​ return dupes;}​try { assert.deepEqual(findDuplicates('The dog is the best'), ['the']);​ console.log( 'PASSED: ' + "`findDuplicates('The dog is the best')` returns `['the']`"OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
Alright, well done! Try another walk-through.
If you had any problems with this tutorial, check out the main forum thread here.


