Skip to content

Instantly share code, notes, and snippets.

@cosmopeon
Last active January 19, 2016 16:23
Show Gist options
  • Save cosmopeon/85b7f6d6556820bb5186 to your computer and use it in GitHub Desktop.
Save cosmopeon/85b7f6d6556820bb5186 to your computer and use it in GitHub Desktop.
function mostCommon (array) {
return array
.filter(function (value, index, array) {
return array.indexOf(value) === index;
})
.map(function(uniqueItem) {
var count = 0;
array.forEach(function(item) {
if (uniqueItem === item) {
count++;
}
});
return {name: uniqueItem, count: count};
})
.sort(function(a, b) {
return b.count - a.count;
})[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment