2

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));
Sur Khosh
  • 115
  • 1
  • 8

6 Answers6

8

i also found this :

function deleteNth(arr,x) {
  var cache = {};
  return arr.filter(function(n) {
    cache[n] = (cache[n]||0) + 1;
    return cache[n] <= x;
  });
}
Sur Khosh
  • 115
  • 1
  • 8
3

I would use a reduce to iterate over all elements of the array and a dictionary to keep track of the number of times I found an element.

Here's an example:

const filterReps = (arr, maxReps) => {
  return arr.length ? arr.reduce((acc, num, i) => {
    // Add this number to our dictionary,
    // if already present add +1 to it's count
    acc.found[num] = acc.found[num] ? ++acc.found[num] : 1

     // If the dictionary says the number has been found less or equal
     // times to our max repetitions, push it into the accumulating array
    if (acc.found[num] <= maxReps)
      acc.arr.push(num)

    // If this is the final iteration, just return only the result array
    // and not the dictionary
    return i === nums.length - 1 ? acc.arr : acc
  }, { found: {}, arr: [] }) : arr
}

const nums = [1, 1, 1, 1, 2, 2, 2, 2]
console.log(filterReps(nums, 3))
nicholaswmin
  • 21,686
  • 15
  • 91
  • 167
1

onece more

let deleteNth = function func(arr,n){ 
    for (let i=0; i<= arr.length; i++){ 
        while( arr.filter( (item) => item == arr[i]).length > n ){ 
           arr.splice(arr.lastIndexOf(arr[i]),1); 
        }
    }
    return arr;
}
GROL COON
  • 43
  • 5
0

You can make this a bit simpler by using just a single function. Here's an example of how it could be done:

function dn(arr,n){
  results = {};
  for(var i=0; i<arr.length; i++){
    if(!results[arr[i]]){
      results[arr[i]] = 1;
    }else if(results[arr[i]] + 1 <= n){
      results[arr[i]]++
    }
  }
  data = []
  Object.keys(results).forEach(function(key){
    for(i=0; i<results[key]; i++){
      data.push(key);
    }
  });
  return data;
}
Anthony L
  • 2,159
  • 13
  • 25
0

Try this one, I think this may help you too.

function removeMoreDups(your_array, limit) {
  new_arr = [];
  var counts = {};
  your_array.forEach(function(x) {
    counts[x] = (counts[x] || 0)+1;
    if (counts[x] <= limit) {
     new_arr.push(x);
    }
  });
  return new_arr;
}

your_array = [1,1,3,3,7,2,2,2,2];
limit = 3;
new_arr = removeMoreDups(your_array, limit);
console.log(new_arr);
Sinto
  • 3,915
  • 11
  • 36
  • 70
0
var itemCounter = ((char, tab) => tab.filter((tabItem) => (tabItem === char)).length);

function deleteNth(arr,n){
  var tempTab = [];
  arr.forEach((item) => {
    if(itemCounter(item,tempTab) < n) 
      tempTab.push(item);
  });
  return tempTab;
}
marqn
  • 81
  • 1
  • 1