Created
February 8, 2011 11:39
-
-
Save Cside/816301 to your computer and use it in GitHub Desktop.
単語の配列から最頻出の単語を返す
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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