I have:
myArray = [\"ABAB\", \"ABAB\", \"ABAB\", \"CDCD\", \"EFEF\", \"EFEF\"]
I need to count by occurrences and sort by highest count. This w
Try this:
array_elements = ["ABAB", "ABAB", "ABAB", "CDCD", "EFEF", "EFEF"];
var result_array = [];
var current = null;
var cnt = 0;
for (var i = 0; i < array_elements.length; i++) {
if (array_elements[i] != current) {
if (cnt > 0) {
result_array.push([current,cnt]);
}
current = array_elements[i];
cnt = 1;
} else {
cnt++;
}
}
if (cnt > 0) {
result_array.push([current,cnt]);
}
result_array.sort(function(x,y) {return y[1] - x[1]})
alert(result_array);
The result_array will have the result.