Skip to content

Instantly share code, notes, and snippets.

@matonga
Created August 11, 2018 14:12
Show Gist options
  • Save matonga/0f013f4ce279e8cdb48440203f820911 to your computer and use it in GitHub Desktop.
Save matonga/0f013f4ce279e8cdb48440203f820911 to your computer and use it in GitHub Desktop.
// 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