Skip to content

Commit fb5339b

Browse files
Time: 83 ms (78.72%), Space: 44.2 MB (71.77%) - LeetHub
1 parent ff86ad8 commit fb5339b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Find Most Common Word
3+
*
4+
* @param {string} paragraph
5+
* @param {string[]} banned
6+
* @return {string}
7+
*/
8+
var mostCommonWord = function(paragraph, banned) {
9+
const matchedWords = paragraph.match(/\w+/g);
10+
const hashmap = {};
11+
12+
let mostCommonWord = '';
13+
let mostCommonWordCount = 0;
14+
15+
matchedWords.forEach(wordString => {
16+
const word = wordString.trim().toLowerCase();
17+
18+
if (!banned.includes(word)) {
19+
if (Object.keys(hashmap).includes(word)) {
20+
hashmap[word] = parseInt(hashmap?.[word] ?? 0) + 1;
21+
} else {
22+
hashmap[word] = 1;
23+
}
24+
25+
if (hashmap[word] > mostCommonWordCount) {
26+
mostCommonWordCount = hashmap[word];
27+
mostCommonWord = word;
28+
}
29+
}
30+
});
31+
32+
return mostCommonWord;
33+
};

0 commit comments

Comments
 (0)