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?
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).
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)