I'm trying to get the highest occurrence of a array value, and if there's an equal occurrence, I should get the first selected value of the equal occurrences.
Example:
var array = ['25', '50', 'a', 'a', 'b', 'c']
In this case I should get a
var array = ['75', '100', 'a', 'b', 'b', 'a']
In this case I also should get a
I've done my fair share of searching and found a couple of helpfull posts, these for example:
Somehow I can't seem to modify these examples to work for my case.
Right now I'm using the code below, but its returning the last selected equal occurrence, instead of the first. (credit https://stackoverflow.com/users/1238344/emissary)
function mostFrequent(array){
return array.sort(function(a,b){
return array.filter(function(v){ return v===a }).length
- array.filter(function(v){ return v===b }).length
}).pop();
}
Any help with this is welcome.