Remove duplicates in an array of numbers – Javascript Coding Question

In a given interview, you may be presented with the following problem:


The problem:

“Build a function that takes in an array of numbers, and then returns the array without any duplicate elements“



The easiest way I found to solve this question has been to loop through the array, then push every item on a new array if it has not been added before.

  1. Since we want to loop through the entire array and never break out, we can use Array.forEach:

    array.forEach(item => {
  2. Inside the loop’s testing function, we push the item into the output array if it this item is not in it already.

    Easy huh? Indeed, this is one of the simpler questions I’ve come across.


Full working example:


let removeDuplicates = array => {
  let output = [];

  array.forEach(item => {
    if (!output.includes(item))


removeDuplicates([1, 2, 3, 2, 4, 4]);

Share your thoughts