Skip to content

Instantly share code, notes, and snippets.

@mindplace
Created March 29, 2016 14:36
Show Gist options
  • Select an option

  • Save mindplace/b30f04954bd02bbd9ee6 to your computer and use it in GitHub Desktop.

Select an option

Save mindplace/b30f04954bd02bbd9ee6 to your computer and use it in GitHub Desktop.
function findMode(array) {
var hashOfValues = {};
for (var i=0; i < array.length; i++) {
var count = 0;
var current = array[i];
for (var j=0; j < array.length; j++) {
var secondCurrent = array[j];
if (secondCurrent === current) {
count += 1;
}
}
hashOfValues[current] = count;
}
var highestCount = 0;
for (var elem in hashOfValues) {
if (hashOfValues[elem] > highestCount) {
highestCount = hashOfValues[elem];
}
}
var items = new Array;
for (var thing in hashOfValues) {
if (hashOfValues[thing] === highestCount) {
items.push(Number(thing));
}
}
console.log(items);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment