Created
August 11, 2018 14:12
-
-
Save matonga/0f013f4ce279e8cdb48440203f820911 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
// input array | |
a = [ 5, 14, 6, 7, 7, 7, 20, 10, 10 ] | |
// build an associative array with each number and count how many times it appears in the input array | |
b = { }, a.forEach ((x) => { b[x] = (b[x]|0)+1;}) | |
// search for the one with maximum count | |
c = Object.keys (b).map ((x) => b[x]) | |
d = Math.max.apply (Math, c) | |
e = c.indexOf (d) | |
f = Object.keys (b)[e] | |
// output result | |
console.log ('Number ' + f + ' appears more frequently') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment