Skip to content

Instantly share code, notes, and snippets.

@Cside
Created February 8, 2011 11:39
Show Gist options
  • Save Cside/816301 to your computer and use it in GitHub Desktop.
Save Cside/816301 to your computer and use it in GitHub Desktop.
単語の配列から最頻出の単語を返す
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment