Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function modalWord(words) {
- // words = words.map(function(s) { return s.toLowerCase(); }); // nomalize
- var counter = {};
- words.forEach(function(str){
- if(counter.hasOwnProperty(str)) {
- counter[str]++;
- } else {
- counter[str] = 1;
- }
- });
- var maxCount = 0;
- var modal;
- for (var keyStr in counter) {
- if (maxCount === 0) {
- modal = keyStr;
- maxCount = counter[keyStr];
- } else {
- if (counter[keyStr] > maxCount) {
- modal = keyStr;
- maxCount = counter[keyStr];
- }
- }
- }
- return modal;
- }
Add Comment
Please, Sign In to add comment