AlgoDaily Solution
1var assert = require('assert');
2
3function findDuplicates(str) {
4 const dupes = [];
5 const strLowerCase = str.toLowerCase();
6 const strArr = strLowerCase.split(' ');
7 const wordFreqCounter = {};
8
9 strArr.forEach((word) => {
10 if (!wordFreqCounter[word]) {
11 wordFreqCounter[word] = 1;
12 } else {
13 wordFreqCounter[word] += 1;
14 }
15 });
16
17 let allKeys = Object.keys(wordFreqCounter);
18
19 allKeys.forEach((key) => {
20 if (wordFreqCounter[key] > 1) {
21 dupes.push(key);
22 }
23 });
24
25 return dupes;
26}
27
28try {
29 assert.deepEqual(findDuplicates('The dog is the best'), ['the']);
30
31 console.log(
32 'PASSED: ' + "`findDuplicates('The dog is the best')` returns `['the']`"
33 );
34} catch (err) {
35 console.log(err);
36}
37
38try {
39 assert.deepEqual(findDuplicates('Happy thanksgiving, I am so full'), []);
40
41 console.log(
42 'PASSED: ' +
43 "`findDuplicates('Happy thanksgiving, I am so full’)` returns `[]`"
44 );
45} catch (err) {
46 console.log(err);
47}
Community Solutions
Community solutions are only available for premium users.
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.
xxxxxxxxxx
30
var assert = require('assert');
function findDuplicates(str) {
const dupes = [];
// fill in
return dupes;
}
try {
assert.deepEqual(findDuplicates('The dog is the best'), ['the']);
console.log(
'PASSED: ' + "`findDuplicates('The dog is the best')` returns `['the']`"
);
} catch (err) {
console.log(err);
}
try {
assert.deepEqual(findDuplicates('Happy thanksgiving, I am so full'), []);
console.log(
'PASSED: ' +
"`findDuplicates('Happy thanksgiving, I am so full’)` returns `[]`"
);
} catch (err) {
OUTPUT
Results will appear here.