Skip to content

Instantly share code, notes, and snippets.

@tarzak
Created February 4, 2016 15:20
Show Gist options
  • Save tarzak/1d3676cdc5778efc9e03 to your computer and use it in GitHub Desktop.
Save tarzak/1d3676cdc5778efc9e03 to your computer and use it in GitHub Desktop.
function countClasses(array) {
var object = {}
;
array.forEach(function(element) {
if (object[element]) {
object[element]++;
}
else {
object[element] = 1;
}
})
return object;
}
function buildArray(object) {
var array = []
, maxProperty
;
while (Object.keys(object).length) {
maxProperty = findMaxProperty(object);
array.push(maxProperty);
delete object[maxProperty];
}
return array;
}
function findMaxProperty(object) {
var max = 0
, maxProperty
, property
;
for (property in object) {
if (object[property] > max) {
max = object[property];
maxProperty = property;
}
}
return maxProperty;
}
var arr = ['xxx','bad','asd','asd', 'asd', 'bad', 'bad'];
buildArray(countClasses(arr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment