Created
April 16, 2014 20:14
-
-
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
This file contains hidden or 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
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