Skip to content

Instantly share code, notes, and snippets.

@germanescobar
Created August 18, 2023 23:58
Show Gist options
  • Save germanescobar/1a6c71d77d495b17e1483cc8ea3d2007 to your computer and use it in GitHub Desktop.
Save germanescobar/1a6c71d77d495b17e1483cc8ea3d2007 to your computer and use it in GitHub Desktop.
var mostCommonWord = function(paragraph, banned) {
const words = paragraph.toLowerCase().split(/\W+/)
const freqs = frequencies(words, banned)
return findMaxWord(freqs)
};
function frequencies(words, banned) {
const freqs = {}
for (let w of words) {
if (w && !banned.includes(w)) {
freqs[w] = freqs[w] ? freqs[w] + 1 : 1
}
}
return freqs
}
function findMaxWord(freqs) {
let max = 0
let maxWord = ""
for (let w in freqs) {
if (freqs[w] > max) {
max = freqs[w]
maxWord = w
}
}
return maxWord
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment