How to find most frequent item of an array? [Updated]
Usama Tahir
263

var cats = [‘Tom’,’Fluffy’,’Tom’,’Bella’,’Chloe’,’Tom’,’Chloe’];

(function(array){
 array = array.slice();
 var a,b,c;
 b = 0;
 c = [];
 d = [];

for(let i = 0; i < array.length;){
 a = array.shift();
 b = 1;

for(let j = 0; j < array.length; j++){
 if(a == array[j]){
 array.splice(j, 1);

j — 
 b++;
 }
 }
 c.push(b);
 d.push(a);
 }

//will return most frequent item in array cats
 return d[c.indexOf(Math.max(…c))];
})(cats);

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.