JS Remove Duplicates The Shortest Way [SOLVED]

remove duplicates

One of the coding interview questions is how to remove duplicates from an array. Of course as for any problem, it has more than one approach in javascript. One of these approaches is to benefit from:

  • Set Object
  • Spread Operator

Here is one of the possible approaches  👇 

function removeDuplicates(arr) {
  return [...new Set(arr)]
}

removeDuplicates([1, 3, 5, 3, 1]); // [1, 3, 5]

Leave a Reply

Your email address will not be published. Required fields are marked *