count occurence
a function tha takes an array and a number(x) and return all the numbers in the array that occurs x-times
by kadonWills
JavaScript
function countOccurence(number, occuranceNumber) {
var counted = [] /*has the counted numbers*/;
var count = [] /*contains the occurence of each number*/;
var i = 0, j = 0, k = 0;
while (k < number.length) {
/**we check if the number has been counted */
if (counted.indexOf(number[k]) < 0) {
counted[i] = number[k];
count[i] = 0;
/**we now count the number's occurence*/
for (j = 0; j < number.length; j++) {
if (counted[i] == number[j]) {
count[i]++;
}
}
if (count[i] < occuranceNumber )
count[i] = null;
i++;
} else {
k++;
}
}
j=0;
while ( j < counted.length){
if (count[j] == occuranceNumber)
document.write(counted[j] +" occurs "+ count[j] + " times <br/>");
j++;
}
}
var number = [3,6,7,4,6,9,3,2,3];
var occuranceNumber = 1;//number of occurances to be checked
countOccurence(number, occuranceNumber);