Remove all Duplicates

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

Remove all Duplicates

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

Problem

/**
* Remove all duplicate items from an array
*/
export function removeDuplicates<T>(items: T[]): T[] {

}

Solution

/**
* Remove all duplicate items from an array
*/
export function removeDuplicates<T>(items: T[]): T[] {
return [...new Set<T>(items)];
}
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

In our previous lesson, we looked at the problem of finding a repeated item within a given input array. A closely related task and something that you will actually use in your real day job is the task of removing all the duplicates from a given input array. And in this lesson, we will look at an I solution along with an efficient solution that uses any J script syntax. So let's go. Permission should you choose to accept it is to take an input array of any type type T and return an output array. With all of the duplicated items removed, here's just an example of what we want this function to do. Given this input array of numbers,

00:33

it should remove all duplicates and there only duplicate over here is the number one, and you can see that it is present only once within the output array. Right now we just have the scaffold for this particular function, and you can think for a solution if you want to. Now, a quick, simple solution and a naive one at that is to look through the items in the input array one by one, and then for each item, first of all, check if you've already added it to the array. And if we have, then continue.

01:05

Otherwise, add this item to the array as this is the first time we've seen it and we haven't found any duplicates for this particular item. Now, what makes this a naive solution is the fact that we are looping through all of the items in the input array, and then we are essentially looping again within the function that is result includes, this will actually go through all of the items that we currently have in the result till it finds one that matches this results in an overall time complexity of ON square, we can definitely do better than that. And the answer lies in the set data structure. The set data structure is essentially designed to make sure

01:40

that the items are unique. So we replace our array based implementation with one that uses the set data structure. In this implementation, we still loop through all of the input items, however, we simply add them to the set. And if it's a duplicate set, data structure will automatically ignore the duplicate. Finally, now that we have a set, we have to convert it back to an array. And the simplest way to do that is to use the built-in radar form function, which can be used to convert any array like structure into a true JavaScript array. Now, as for the time complexity

02:11

of this particular implementation, we are looping through all of the items still. So that's all FN, but within the loop there is OF one because the result add is an OF one operation resulting in overall OFN. Now, a neat thing about the JavaScript set data structure is that you can pass in an input array to the set constructor and the constructor will essentially loop through that input array and add all of those items uniquely to the set. Now there is no point in creating a variable if you're only going to use it once. So we are going to inline the set constructor.

02:43

Now, one final tweak that we are going to make over here is to utilize the fact that in addition to array form, JavaScript provides an alternative way for creating an array from any given Iterable, and that syntax is known as a JavaScript spread syntax. We create a new Array and then we spread in our set Iterable into this array, and this is the final array that we will return. The set will take the input items, convert them into a unique collection of items, and then we will iterate over this unique collection and we will add them into a new array, which we will finally return according interview questions.

03:18

Go. Removing duplicates from a given input array is a pretty easy one to solve, but then again, many programming challenges are a few seen a decent solution.