I am taking the LeetCode challenge 819. Most Common Word:
Given a string
paragraphand a string array of the banned wordsbanned, return the most frequent word that is not banned. It is guaranteed there is at least one word that is not banned, and that the answer is unique.The words in
paragraphare case-insensitive and the answer should be returned in lowercase.Example 1
Input:
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.",banned = ["hit"]
Output:"ball"
This is my code:
var mostCommonWord = function(paragraph, banned) {
let words = paragraph.toLowerCase().split(" ");
let unbannedwords = words.filter((word)=> word !== banned[0] );
let map = {};
console.log(unbannedwords)
for (let i = 0; i < unbannedwords.length; i++) {
count = 0;
for (let j = i + 1; j < unbannedwords.length; j++) {
if (unbannedwords[i] === unbannedwords[j]) {
count++;
map[unbannedwords[i]] = count;
}
}
}
console.log(map)
let obj = Object.values(map)
let max = Math.max(...obj);
function getKeyByValue(object, value) {
return Object.keys(object).find(key => object[key] === value);
}
return getKeyByValue(map, max);
};
Output console.log:
[
'bob', 'a',
'ball', 'the',
'ball', 'flew',
'far', 'after',
'it', 'was'
]
{ }
I don't why properties are not being from the loop in map object?