Skip to content

Instantly share code, notes, and snippets.

@Luke-Rogerson
Created September 23, 2018 14:43
Show Gist options
  • Save Luke-Rogerson/f77bbb351025a6f500559b6cd388ab9b to your computer and use it in GitHub Desktop.
Save Luke-Rogerson/f77bbb351025a6f500559b6cd388ab9b to your computer and use it in GitHub Desktop.
mostFrequentWordChallenge created by Luke_Rogerson - https://repl.it/@Luke_Rogerson/mostFrequentWordChallenge
const _ = require('lodash');
function highestWordCount (str) {
let resultArray = str.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()0-9]/g, "").split(' ');
let words = {};
for (word of resultArray) {
let counter = 0;
for (word2 of resultArray) {
if (word === word2) {
counter++;
words[word] = counter
}
}
}
let sortedByValue = _.transform (words, function(result, value, key) {
(result[value] || (result[value] = [])).push(key);
}, {});
return (sortedByValue[Object.keys(sortedByValue)[Object.keys(sortedByValue).length - 1]]);
}
highestWordCount('I see, mmm... I see, 2 and 2.');
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"lodash": {
"version": "4.17.11",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz",
"integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg=="
}
}
}
@Luke-Rogerson
Copy link
Author

A simple function to return the most frequent word (or words) in a string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment