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

Popular posts from this blog

ruby - When to use an ORM (Sequel, Datamapper, AR, etc.) vs. pure SQL for querying -

php - PHPDoc: @return void necessary? -

c++ - Convert big endian to little endian when reading from a binary file -