i'm trying to create a function that gives me an array of numbers with a limitation of repetition for each number. for example
([1,1,3,3,7,2,2,2,2], 3)
should give me
[1, 1, 3, 3, 7, 2, 2, 2]
it deletes a [2] because the max repetition of numbers is 3.
here is my code but i don't know why it doesn't work:
function deleteNth(arr,n){
var results = [];
for(var i=0; i<arr.length; i++){
if (count(results, arr[i])<=n) {
results.push(arr[i]);
}
}
return results;
}
function count(array, what){
var count =0;
for (var i=0; i<array.length; i++){
if (array[i]===what){
count++;
}
}
return count;
}
console.log(deleteNth([1,1,3,3,7,2,2,2,2], 3));