Javascript Array numberOfOccurrences(x)
Array.prototype.numberOfOccurrences = function(x) { var counter = 0; for(var i = 0; i < this.length; i++) { if(this[i] == x){ counter++;//from w w w . j a v a 2 s. c o m } } return counter; }
Array.prototype.numberOfOccurrences = function(x) { var freq = {}; for (var i = 0; i < this.length; i++){ if (freq[this[i]]) { freq[this[i]]++;//from ww w . j a va2 s . c om } else { freq[this[i]] = 1; } } if(freq[x]){ return freq[x]; }else return 0; } var arr = [0,1,2,2,3]; arr.numberOfOccurrences(0)/* === 1*/; arr.numberOfOccurrences(4) /* === 0*/; arr.numberOfOccurrences(2) /*=== 2*/; arr.numberOfOccurrences("a") /*=== 0*/;