Skip to content

Instantly share code, notes, and snippets.

@agmcleod
Created April 16, 2014 20:14
Show Gist options
  • Save agmcleod/10928358 to your computer and use it in GitHub Desktop.
Save agmcleod/10928358 to your computer and use it in GitHub Desktop.
Check for value in string array that has highest count, or collect what the highest ties with
var answers = [
"A",
"B",
"C",
"C"
];
answers.sort();
var types = {};
for(var i = 0; i < answers.length; i++) {
types[answers[i]] = (types[answers[i]] || 0) 1;
}
var top = 0;
var winner = null;
for(var key in types) {
if(types.hasOwnProperty(key)) {
var count = types[key];
if(count > top) {
top = count;
winner = key;
}
}
}
var tie = [];
for(var key in types) {
if(types.hasOwnProperty(key)) {
if(key !== winner && types[key] === top) {
tie.push(key);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment