You might also get a task like this, remove all duplicates in their right, which basically means if
you have an array of numbers and you have, for example, one one tool, you need to live on the one
and tongue.
I see here three different ways to solve our problem.
Let's try with the best solution.
For example, we can create here a function unique array, and we're getting here our way to remove
all duplicates.
And here what we want to do.
We want to return and jewelry and we want to spread inside.
And you said and were bison inside the rate.
If you don't know what certain JavaScript is, it's an object which allow you to store unique values
of any type.
This means that if we're right in here and you said and were passing inside out, for example, one
one tomb were hidden.
And as you can see, we have the set only from one and two because inside those set elements can be
duplicated.
This is why, as you can see if we will spread on your set and put it in the new array to convert said
back to us.
We will get our unique elements.
And actually not a lot of people know about this solution, but this is the best solution because it
is, first of all, a single line.
Secondly, it is really performant.
If you don't know this solution, it's not bad.
You can always build this logic on your own.
The most important part that even for loop here, if you are using for reach for Ray is not that bad.
Let's check this out.
For example, we have here the same function, unique array, and we have one two right here, a loop.
And of course, we need to store our result in additional property.
So let's make here result, which will be our unique right now.
Here, I want to look through every single element of our array with for each function.
And here we're getting our item as every element of our array.
Now what we want to check here, if we already have the system inside our result, so we can check here
result dot includes.
And if you don't know, includes check if we have the element inside in the rape and we want to pass
inside our item.
So this will return true if it includes.
But actually, we must put here bank to have a negation.
And if it does not include this element inside the array, then we want to push it to our result.
So result Bush and here the item.
So as you can see, even for each look here is not that bad because it is isolated inside our function
where simpler looping through every single element and we're pushing our result directly in the new
array.
And of course, the we must return our result.
So here, let's return the result and check if it's working.
So here is our function unique array one one two.
And as you can see, we're getting one and two back, but obviously our function won't work with objects
or raise inside.
And this is completely normal.
So if you have here an object with a equals one and here you have one more object with eight equals
one.
As you can see, we're getting two elements and this is totally fine because this is two different objects
and we can't compare them.
And actually, we can provide this for each by using reduce.
In this way, people write exactly the same code buttons and more functional way.
So here let's create our function unique array we're getting here.
And one directly to return array reduce.
And here inside, we must pass our function with accumulator and every single element.
So here we have a function and as a default argument we have here, empty array exactly like we had
here were without constant result equals empty array.
Now what we want to do inside, we want to check our accumulator includes, so we're checking our result.
If we have this resulted, array our element.
So if we have it then was simpler to turn accumulator, so we don't change anything.
In the case, we want to push this element to accumulator and we're not using a push, but we're using
spread.
So here I am spreading accumulator and then I imagine our element in this way.
This code is completely functional.
It is easy to understand and we don't have this nested, but here let's load the page and check if it's
working.
As you can see, it still doesn't work with objects, which is totally fine, and it works with primitives
because here one was removed from our rate.
So as you can see, you have three different variants to implement uniqueness in array.
And actually, all these revisions are completely fine to show on the interview.