AFFILIATE LINKS
Great resource I use to learn algorithms.
40% off Tech Interview Pro: http://techinterviewpro.com/terriblewhiteboard
20% off CoderPro: http://coderpro.com/terriblewhiteboard
Here is the full implementation.
/**
* @param {string} paragraph
* @param {string[]} banned
* @return {string}
*/
let mostCommonWord = function(paragraph, banned) {
let validWordFrequency = {};
let bannedWords = new Set();
let mostCommon = '';
let words = paragraph.split(/\W+/);
for (let word of banned) {
bannedWords.add(word);
}
for (let word of words) {
let lowerCaseWord = word.toLowerCase();
if (!bannedWords.has(lowerCaseWord)) {
if (validWordFrequency[lowerCaseWord] === undefined) {
validWordFrequency[lowerCaseWord] = 0;
}
validWordFrequency[lowerCaseWord]++;
if (mostCommon === '' || validWordFrequency[lowerCaseWord] > validWordFrequency[mostCommon]) {
mostCommon = lowerCaseWord;
}
}
}
return mostCommon;
};