javascript - Get the item that appears the most times in an array -
var store = ['1','2','2','3','4'];
i want find out 2
appear in array. how go doing that?
i like:
var store = ['1','2','2','3','4']; var frequency = {}; // array of frequency. var max = 0; // holds max frequency. var result; // holds max frequency element. for(var v in store) { frequency[store[v]]=(frequency[store[v]] || 0)+1; // increment frequency. if(frequency[store[v]] > max) { // frequency > max far ? max = frequency[store[v]]; // update max. result = store[v]; // update result. } }
Comments
Post a Comment