DEV Community

Discussion on: Daily Challenge #208 - Delete Occurrences of an Element

Collapse
 
mellen profile image
Matt Ellen

Javascript Map:

function onlyN(arr, N)
{
  let countMap = new Map();
  return arr.filter(el =>
  {
    if(countMap.has(el))
    {
      countMap.set(el, countMap.get(el)+1)
    }
    else
    {
      countMap.set(el, 1);
    }
    return countMap.get(el) <= N;
  });
}