I'm using the following function to get the most frequent item of an array:
Array.prototype.array_most_frequent = function() {
var m=0, mf=0;
for (var i=0; i<this.length; i++) {
for (var j=i; j<this.length; j++)
{
if (this[i] == this[j]) m++;
if (mf<m) {mf=m; item = this[i]; }
}
m=0;
}
return item;
}
Works fine - but how to get the rarest item of an array?
For example var array=[2,1,2,5,5]
should return '1
'.
Is there an easy way to the least frequent item of my array?
Thanks in advance.