Remove Duplicates from Array

Hi everyone,

I am trying to solve the problem of removing duplicates from an array. I have come up with a solution, but I am not sure if it is the most efficient way to do it.

My solution is as follows:

function removeDuplicates(arr) {
  let newArr = [];
  for (let i = 0; i < arr.length; i++) {
    if (!newArr.includes(arr[i])) {
      newArr.push(arr[i]);
    }
  }
  return newArr;
}

This code works by first creating a new empty array. Then, it iterates over the original array and adds each element to the new array if it is not already in the new array. Finally, it returns the new array.

I am not sure if this is the most efficient way to do it. Is there a more efficient way to remove duplicates from an array?

Thank you.

This is a nested loop.

The usual way around this is to use a hash set

Your solution is functional and will indeed remove duplicates from an array. However, its efficiency is not the best, especially for larger arrays. The includes method has a time complexity of O(n), and since you’re calling it inside a loop that runs for the length of the input array, the overall time complexity of your solution is O(n^2).

A more efficient approach is to use a hash set (implemented in JavaScript using an object or a Map) to keep track of elements you’ve encountered. Check this article out to know more so this way, you can achieve a linear time complexity of O(n).

It’s not quite the same n in each O(n) you are talking about in those two parts - gotta be careful to say what n means.

There is no direct nested loop, but the includes method takes O(n) time to check if element is alteady present. Although the internal implementation of includes uses a loop to search.
So overall time complexity is O(n^2)

Correct,
Its not O(n) for each value. The growth rate of algorithm will be like -

1 + 2 + 3 + . . . +(n-1) searches in worst case.

Which can be represented as - (n * (n-1))/2

The growth rate matters here, and the degree of this polynomial is O(n^2).

It is a direct nested loop. .includes is a loop

Right… So it’s a 'direct nested loops :upside_down_face:

I don’t know if ‘growth rate’ is the right term, but your math is right

You can also use a Set which requires you to write zero logic.

1 Like

I second this - a set’s default behaviour is to de-dup the collection.

Just don’t forget to create a new array from the set to pass back - otherwise the caller is going to get a bit of a shock.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.