WEBVTT 00:08.080 --> 00:13.420 In this video you're going to have a quick challenge where you will need to create the intersection 00:13.720 --> 00:15.830 and difference of two sets. 00:16.270 --> 00:21.310 So we're gonna use our set my sets and my set to. 00:21.560 --> 00:28.570 And basically what you need to do is create a third set which will be the intersection of my sets and 00:28.570 --> 00:29.550 my set too. 00:29.560 --> 00:38.010 Meaning that the new set is going to include only the common values between my set and my set too. 00:38.080 --> 00:45.640 And then I'm gonna create another set which will include all the items which are not common between 00:45.820 --> 00:48.200 my sets and my set too. 00:48.280 --> 00:48.790 So 00:52.410 --> 00:53.520 this is gonna be the challenge. 00:53.790 --> 00:54.750 So intersection 00:57.510 --> 00:59.490 and difference. 00:59.490 --> 01:05.040 So other moments you've got my set and you've got my set to 01:07.730 --> 01:15.470 so you need to create int set and diff set. 01:15.530 --> 01:21.710 So a quick hint here you're gonna be using the method filter of arrays. 01:21.910 --> 01:22.640 All right. 01:22.820 --> 01:24.550 To solve this problem. 01:24.560 --> 01:27.110 So a quick reminder. 01:27.610 --> 01:28.760 So if you've got an array. 01:28.770 --> 01:30.730 1 2 3 for example. 01:30.740 --> 01:34.450 And you use the filter method. 01:40.190 --> 01:51.820 You pass to filter a function which will perform a test on these values and then he's gonna return true 01:52.000 --> 01:53.400 or false. 01:53.440 --> 01:55.330 So for example let's say that's the function 01:58.090 --> 02:00.610 because it is using the arrow function 02:04.730 --> 02:15.380 so each item let's say that it is going to return whether the item is greater than 2 or not. 02:15.480 --> 02:19.170 So in this case we're going to go through the elements. 02:19.250 --> 02:22.790 So the first one doesn't fulfill the condition. 02:22.790 --> 02:25.250 The second one doesn't. 02:25.280 --> 02:26.870 And the third one does. 02:26.900 --> 02:30.790 So the final array is going to show only the last one. 02:30.830 --> 02:31.390 All right. 02:31.430 --> 02:42.800 So we if we test this we get an array of 1 Elements 3 so it is a quick reminder of the method filter. 02:42.860 --> 02:50.230 So I suggest you pause the video here and solve the problem and then come back once you're done. 02:50.280 --> 02:50.560 All right. 02:50.560 --> 02:52.900 So hopefully you manage to find the solution. 02:52.900 --> 03:00.730 So as you expect we're gonna need to work with arrays here because we need to use the method filter 03:01.030 --> 03:02.670 of arrays. 03:02.740 --> 03:14.190 Now what we can do is we can transform my sets on the first sets to an array and then use the filter 03:14.280 --> 03:21.220 method to check if the elements of the array are inside my set too. 03:21.720 --> 03:25.530 So we can create 03:28.830 --> 03:29.420 a new array. 03:29.430 --> 03:32.610 So let's call it int array like that's 03:36.020 --> 03:39.920 this array is going to be the outputs of the filter method. 03:40.040 --> 03:44.270 So let's transform my set to an array using these spread operator 03:49.070 --> 03:58.990 and then use the filter method to check if the elements inside this array are inside my set too. 03:59.120 --> 04:00.850 And as you know to do that 04:05.900 --> 04:09.640 we're going to use the US method of the set 04:13.340 --> 04:18.330 to checking if the item is inside my set too. 04:18.350 --> 04:25.100 So now let's log the array and see what we get. 04:25.140 --> 04:27.310 Here we go. 04:27.360 --> 04:29.120 So just to make things clear. 04:29.220 --> 04:32.250 Let's just let's just log my sets and my set to 04:39.930 --> 04:45.060 so that's my set and that's my set too and you can see that the common values are 1. 04:45.180 --> 04:47.820 And John and here is our array. 04:47.850 --> 04:49.890 Now we want a set. 04:50.010 --> 04:57.200 So it is create a sets int set from the array. 04:57.540 --> 05:04.020 And as you know you can pass an interval to the says constructor. 05:04.070 --> 05:08.880 Here we go if we log in sets 05:11.980 --> 05:13.870 you get the right results. 05:13.900 --> 05:20.170 So you've got a sense there and that's going to be the instruct the intersection between SET ONE AND 05:20.230 --> 05:21.500 SET TWO. 05:21.520 --> 05:27.310 NOW WE GONNA DO THE SAME THING TO CREATE THE DIFFERENCE. 05:30.070 --> 05:33.960 AND THE CODE IS GONNA it's going to be very similar so we're going to we're going to create an array 05:34.350 --> 05:37.700 and then we're gonna use the array to create a new set. 05:37.750 --> 05:42.050 So I'm going to copy the code and just and just change it slightly. 05:42.240 --> 05:48.330 So this was the intersection and this is gonna be the difference. 05:48.330 --> 05:52.140 So creating a different rate. 05:52.500 --> 05:53.340 So we are going 05:56.420 --> 05:58.810 through the first set 06:01.990 --> 06:07.240 after transforming it to an array and we using the filter method. 06:07.830 --> 06:19.000 And this time we are looking for items which are not inside set too. 06:19.120 --> 06:27.470 So we are returning the opposite of that all right. 06:27.760 --> 06:38.410 So it's a long diff array and then let's create diff sets using diff array 06:43.470 --> 06:48.180 so let's see what we get. 06:48.190 --> 06:57.130 So now you can see that we are getting the objects and body and these are the different items or items 06:57.130 --> 07:06.480 which are not common between the first sets and the second set. 07:06.610 --> 07:08.300 There is a problem here. 07:08.420 --> 07:17.400 We don't see the value high which is also not common between the two sets. 07:17.570 --> 07:23.690 And that's because we only checked the values inside the first set and made sure that we pick the ones 07:23.750 --> 07:25.360 which are not common. 07:25.430 --> 07:28.130 What we didn't do that's for the second sets. 07:28.220 --> 07:38.260 So what we need to do we need to do the same thing for the second sets so I'm going to copy the same 07:38.260 --> 07:38.590 code 07:42.690 --> 07:44.350 and create a new diff array. 07:45.710 --> 07:51.580 And this time we're going to do that to the second set and we check in if the items are inside the first 07:51.670 --> 07:54.230 set we look at. 07:54.230 --> 08:03.590 We're going to log the new array and they're going to create a new diff sets out of the new diff array 08:04.460 --> 08:06.800 and let's log it. 08:07.100 --> 08:09.090 And now we get high. 08:09.170 --> 08:12.760 So you've got two sets here for the difference. 08:13.070 --> 08:13.860 That's one. 08:13.870 --> 08:14.980 And that's one. 08:14.980 --> 08:15.290 All right. 08:15.620 --> 08:23.900 So now we want to combine these two sets to create our final different sets. 08:23.900 --> 08:28.010 Now instead of creating a div sets every time 08:32.440 --> 08:43.180 we can do something more interesting and create only one deep set out of the two arrays which we created 08:43.840 --> 08:44.320 so far. 08:44.950 --> 08:51.670 So you've got that array and that array and all we need to do is combine the two arrays inside a set 08:54.400 --> 08:58.850 so we can pass to the second constructor both arrays. 08:58.870 --> 09:06.100 So let's try those and see if it works and it doesn't work right. 09:06.140 --> 09:10.730 Because we can't see past two arrays to the set constructor like this. 09:10.730 --> 09:12.590 So you need to pass only one array. 09:13.010 --> 09:17.810 So what we need to do we need to pass one array which is gonna be the concatenation of the two arrays 09:18.200 --> 09:35.120 so you can do that by going to the first array and using the Concord method to which you pass the secondary. 09:35.140 --> 09:35.540 All right. 09:35.540 --> 09:41.110 So now you create you creating a new array which is gonna be the combination of the two. 09:41.270 --> 09:45.270 And then you are passing that to the set construct. 09:45.290 --> 09:48.680 And let's see if it works this line and it does work. 09:48.800 --> 09:54.200 And this is the difference between the two sets. 09:54.200 --> 09:54.470 All right. 09:54.470 --> 09:59.050 So now hopefully you have a really good understanding of sets in Essex. 09:59.150 --> 10:02.860 So in the next video you're going to learn symbols.