1function f(array,value){
2 var n = 0;
3 for(i = 0; i < array.length; i++){
4 if(array[i] == value){n++}
5 }
6 return n;
7}
8
1Array.prototype.frequencies = function() {
2 var l = this.length, result = {all:[]};
3 while (l--){
4 result[this[l]] = result[this[l]] ? ++result[this[l]] : 1;
5 }
6 // all pairs (label, frequencies) to an array of arrays(2)
7 for (var l in result){
8 if (result.hasOwnProperty(l) && l !== 'all'){
9 result.all.push([ l,result[l] ]);
10 }
11 }
12 return result;
13};
14
15var freqs = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4].frequencies();
16alert(freqs[2]); //=> 5
17// or
18var freqs = '1,1,2,one,one,2,2,22,three,four,five,three,three,five'
19 .split(',')
20 .frequencies();
21alert(freqs.three); //=> 3