Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
122
rated 0 times [  123] [ 1]  / answers: 1 / hits: 25716  / 14 Years ago, thu, august 26, 2010, 12:00:00

What is an elegant way to take a javascript array, order by the frequency of the values, and then filter for uniques?



So,



[apples, oranges, oranges, oranges, bananas, bananas, oranges]



becomes



[oranges, bananas, apples]


More From » arrays

 Answers
3

Compute the frequency of each item first.



{
apples: 1,
oranges: 4,
bananas: 2
}


Then create an array from this frequency object which will also remove the duplicates.



[apples, oranges, bananas]


Now sort this array in descending order using the frequency map we created earlier.



function compareFrequency(a, b) {
return frequency[b] - frequency[a];
}

array.sort(compareFrequency);


Here's the entire source (using the newly introduced Array functions in ECMA 5) and combining the de-duplication and frequency map generation steps,



function sortByFrequency(array) {
var frequency = {};

array.forEach(function(value) { frequency[value] = 0; });

var uniques = array.filter(function(value) {
return ++frequency[value] == 1;
});

return uniques.sort(function(a, b) {
return frequency[b] - frequency[a];
});
}


Same as above using the regular array iteration.



function sortByFrequencyAndRemoveDuplicates(array) {
var frequency = {}, value;

// compute frequencies of each value
for(var i = 0; i < array.length; i++) {
value = array[i];
if(value in frequency) {
frequency[value]++;
}
else {
frequency[value] = 1;
}
}

// make array from the frequency object to de-duplicate
var uniques = [];
for(value in frequency) {
uniques.push(value);
}

// sort the uniques array in descending order by frequency
function compareFrequency(a, b) {
return frequency[b] - frequency[a];
}

return uniques.sort(compareFrequency);
}

[#95793] Tuesday, August 24, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darleneh

Total Points: 231
Total Questions: 110
Total Answers: 94

Location: Spain
Member since Thu, Dec 23, 2021
2 Years ago
;