1
00:00:00,390 --> 00:00:06,900
You might also get a task like this, remove all duplicates in their right, which basically means if

2
00:00:06,900 --> 00:00:12,330
you have an array of numbers and you have, for example, one one tool, you need to live on the one

3
00:00:12,330 --> 00:00:12,930
and tongue.

4
00:00:16,220 --> 00:00:19,310
I see here three different ways to solve our problem.

5
00:00:19,550 --> 00:00:21,380
Let's try with the best solution.

6
00:00:21,650 --> 00:00:27,860
For example, we can create here a function unique array, and we're getting here our way to remove

7
00:00:27,860 --> 00:00:28,670
all duplicates.

8
00:00:28,970 --> 00:00:30,290
And here what we want to do.

9
00:00:30,290 --> 00:00:34,520
We want to return and jewelry and we want to spread inside.

10
00:00:34,520 --> 00:00:37,730
And you said and were bison inside the rate.

11
00:00:38,030 --> 00:00:43,730
If you don't know what certain JavaScript is, it's an object which allow you to store unique values

12
00:00:43,730 --> 00:00:44,720
of any type.

13
00:00:45,020 --> 00:00:50,840
This means that if we're right in here and you said and were passing inside out, for example, one

14
00:00:50,840 --> 00:00:52,400
one tomb were hidden.

15
00:00:52,400 --> 00:00:58,550
And as you can see, we have the set only from one and two because inside those set elements can be

16
00:00:58,550 --> 00:00:59,240
duplicated.

17
00:00:59,450 --> 00:01:05,330
This is why, as you can see if we will spread on your set and put it in the new array to convert said

18
00:01:05,330 --> 00:01:05,900
back to us.

19
00:01:06,320 --> 00:01:08,300
We will get our unique elements.

20
00:01:08,540 --> 00:01:14,210
And actually not a lot of people know about this solution, but this is the best solution because it

21
00:01:14,210 --> 00:01:15,680
is, first of all, a single line.

22
00:01:15,860 --> 00:01:17,840
Secondly, it is really performant.

23
00:01:18,110 --> 00:01:20,390
If you don't know this solution, it's not bad.

24
00:01:20,540 --> 00:01:23,030
You can always build this logic on your own.

25
00:01:23,270 --> 00:01:28,940
The most important part that even for loop here, if you are using for reach for Ray is not that bad.

26
00:01:29,240 --> 00:01:30,190
Let's check this out.

27
00:01:30,200 --> 00:01:35,600
For example, we have here the same function, unique array, and we have one two right here, a loop.

28
00:01:35,780 --> 00:01:39,260
And of course, we need to store our result in additional property.

29
00:01:39,440 --> 00:01:43,520
So let's make here result, which will be our unique right now.

30
00:01:43,520 --> 00:01:48,650
Here, I want to look through every single element of our array with for each function.

31
00:01:48,890 --> 00:01:52,850
And here we're getting our item as every element of our array.

32
00:01:53,120 --> 00:01:58,880
Now what we want to check here, if we already have the system inside our result, so we can check here

33
00:01:58,880 --> 00:02:00,890
result dot includes.

34
00:02:01,130 --> 00:02:06,800
And if you don't know, includes check if we have the element inside in the rape and we want to pass

35
00:02:06,800 --> 00:02:07,940
inside our item.

36
00:02:08,060 --> 00:02:10,490
So this will return true if it includes.

37
00:02:10,730 --> 00:02:14,120
But actually, we must put here bank to have a negation.

38
00:02:14,330 --> 00:02:19,520
And if it does not include this element inside the array, then we want to push it to our result.

39
00:02:19,760 --> 00:02:22,100
So result Bush and here the item.

40
00:02:22,490 --> 00:02:28,580
So as you can see, even for each look here is not that bad because it is isolated inside our function

41
00:02:28,730 --> 00:02:34,310
where simpler looping through every single element and we're pushing our result directly in the new

42
00:02:34,310 --> 00:02:34,760
array.

43
00:02:35,090 --> 00:02:38,030
And of course, the we must return our result.

44
00:02:38,060 --> 00:02:41,330
So here, let's return the result and check if it's working.

45
00:02:41,630 --> 00:02:44,420
So here is our function unique array one one two.

46
00:02:44,540 --> 00:02:50,600
And as you can see, we're getting one and two back, but obviously our function won't work with objects

47
00:02:50,600 --> 00:02:51,620
or raise inside.

48
00:02:51,770 --> 00:02:53,330
And this is completely normal.

49
00:02:53,540 --> 00:02:59,300
So if you have here an object with a equals one and here you have one more object with eight equals

50
00:02:59,300 --> 00:02:59,590
one.

51
00:02:59,810 --> 00:03:05,390
As you can see, we're getting two elements and this is totally fine because this is two different objects

52
00:03:05,540 --> 00:03:07,100
and we can't compare them.

53
00:03:07,460 --> 00:03:11,030
And actually, we can provide this for each by using reduce.

54
00:03:11,240 --> 00:03:15,860
In this way, people write exactly the same code buttons and more functional way.

55
00:03:15,890 --> 00:03:19,460
So here let's create our function unique array we're getting here.

56
00:03:20,330 --> 00:03:23,750
And one directly to return array reduce.

57
00:03:24,020 --> 00:03:29,090
And here inside, we must pass our function with accumulator and every single element.

58
00:03:29,510 --> 00:03:35,720
So here we have a function and as a default argument we have here, empty array exactly like we had

59
00:03:35,720 --> 00:03:38,960
here were without constant result equals empty array.

60
00:03:39,350 --> 00:03:45,140
Now what we want to do inside, we want to check our accumulator includes, so we're checking our result.

61
00:03:45,260 --> 00:03:48,200
If we have this resulted, array our element.

62
00:03:48,410 --> 00:03:52,460
So if we have it then was simpler to turn accumulator, so we don't change anything.

63
00:03:52,670 --> 00:03:58,580
In the case, we want to push this element to accumulator and we're not using a push, but we're using

64
00:03:58,580 --> 00:03:59,060
spread.

65
00:03:59,360 --> 00:04:04,610
So here I am spreading accumulator and then I imagine our element in this way.

66
00:04:04,610 --> 00:04:06,560
This code is completely functional.

67
00:04:06,710 --> 00:04:11,840
It is easy to understand and we don't have this nested, but here let's load the page and check if it's

68
00:04:11,840 --> 00:04:12,260
working.

69
00:04:12,440 --> 00:04:17,810
As you can see, it still doesn't work with objects, which is totally fine, and it works with primitives

70
00:04:17,810 --> 00:04:20,420
because here one was removed from our rate.

71
00:04:20,750 --> 00:04:25,370
So as you can see, you have three different variants to implement uniqueness in array.

72
00:04:25,370 --> 00:04:29,660
And actually, all these revisions are completely fine to show on the interview.
