DEV Community

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

Collapse
 
davidcoroian profile image
David

Just joined the community! I guess reducer is overkill here but still 🤷

let elements = {};

function deleteNth(lst, n) {
  const reducer = (acc, curVal) => {
    if (elements[curVal]) {
      elements[curVal] += 1;
      if(elements[curVal] <= n) {
        acc.push(curVal)
      }
    } else {
      elements[curVal] = 1;
      acc.push(curVal);
    }
    return acc
  }

  const newlst = lst.reduce(reducer, [])




  console.log(newlst)
}
Collapse
 
avalander profile image
Avalander

Hmm... I wonder what happens if deleteNth is invoked more than once :P

Collapse
 
davidcoroian profile image
David

haha, you re right! Missed that one. Moved the obj initialisation inside the func.