- 2 days ago
Transcript
00:00Next, we will talk about the question. It's called Union and Intersection of two Arrays. Our sets are in math.
00:08If I have a set 1 given and a set 2 given, then set 1 and set 2, what is
00:17union? How many unique elements are in each set?
00:23The number of unique elements, the number of unique elements will become a union of two sets. And what is
00:30the intersection? Set 1 and set 2 is the intersection of how many common elements are in each set. The
00:40students from math background, hopefully they will know that they won't come. Let's say I have number 1, 2, 3.
00:47And in the other set, I have number 4, 5, 1.
00:50So, what will be the union? It will be the union between the two distinct or unique elements. Like the
00:56unique elements are 1, 2, 3, 4 and 5. And what will be the intersection? It will be the common
01:02elements. Common is only here 1.
01:05So, the intersection will be equal to 1. What should I do? I have two arrays given. I can imagine
01:11that there are two sets given or random numbers given. For both, I have to print how many number of
01:17elements will come in union and how many number of elements will come in intersection. For example, if I will
01:23dry run on my given problem, I have array 1 given. I take array 1 and imagine as a set.
01:30In this element is 7.
01:323 and 9. Besides, I have given an array 2. In array 2, there are elements 6, 3, 9, 2,
01:394 and another 9. Now what do I have to do? I have to remove its union. So basically, what
01:46will be union? Both of the combined unique elements. Here will be unique elements. 7, 3, 9. Before I check
01:53here, 6, 6. Add 6. Add 3. 3 already. 9, 9 already. 2. Add 2.
02:00Add 4 to 2, 2 already exists. So the number of elements in my union will be equal to 1,
02:072, 3, 4, 5, 6. This will be equal to 6. I will talk about the intersection. What are the
02:12common elements? These 3 are common. These 9 are common with 9. Here, another 9 exists.
02:20So instead of this 2, I should have 9 here. If this 9 has 2 times, but it doesn't exist
02:26again here, so I don't have to take it in my intersection. What will happen in the intersection? Only unique
02:30elements will come in 3 and then 9. If there is another 9, then I don't count it in the
02:36intersection. Because in the intersection, the elements are distinct and unique. So I will print the distinct elements in 3
02:43and 9, which is equal to 2. This is the same answer that I have here.
02:50and this same answer I have to take out using sets. Now, I can solve this problem. There will be
02:57some of these problems in which we will sort the array. After sorting the array in ascending order, this array
03:04will be 3, 7, 9. This will be 2, 3, 4, 6, 9 and 9.
03:11After that, I will remove the unique elements of sorted arrays. So in that process, basically, I will sort both
03:18arrays as much time I will take. Which is equal to n log n plus m log m. So I
03:25will take some time in this way. Or I can solve this question in n square. But using sets, I
03:31am going to solve this question in my sorting time complexity. So big of n plus m. Where n is
03:38my array 1 and m is my array 2.
03:41How do we use the sets? If we talk about our last question, what did we do in the last
03:46question? For any array, I have removed its distinct elements. Now what do I have to do? For two arrays,
03:52I have removed its distinct elements, meaning its union. So what can I say? The union is very easy. How
03:59is it easy? First, let's add all the elements in a set.
04:05After that, add all the elements of array 2 into the set. Now finally, the size of the set will
04:11be the size of our union set. In my union, there will be so many elements as I add the
04:18elements of array 1 into the set and the elements of array 2 into the set. Because the set will
04:23be only distinct and unique. And then the count will be equal to my set of size. For example, if
04:30I take a single set,
04:32In this first we add the elements of array 1, 7 will add, after that I will add 3, after
04:38that I will add 9, after that I will add 6, after that 3 already exists, 9 already exists,
04:47It doesn't exist, so add 2, 9 already exists, 4 doesn't exist, so I will add 4. So now my
04:53set of size will come, that value is equal to 6, so I will print in my answer, union as
05:006. So with set, I have spent how much time? With set, that will be big O of n plus
05:05m. Now let's talk about the intersection. Intersection will be a little unique. What will I do in the intersection?
05:12For intersection, I will
05:15I will add all the elements in my set. What did I do? I took my set. Now I have
05:21to check what is common between both. What can I do? I will add one. Then I will compare the
05:26other elements. Do they exist in the first one? If they exist, I mean I got common. If they don't
05:33do it, I mean that the elements are not common. If I add the elements of array 1, I will
05:39add 7. Then I will add 3. Then I will add
05:449 will be added. Now I am going to array 2. What am I going to do to array 2?
05:48I am going to add loop. The first step will be to add all elements of array 1. The second
05:56step will be to create a loop which will be i is equal to 0 to array 2 length. And
06:03I will check for array elements if they exist in this set.
06:09So, first of all, let's talk about 6. For 6, I will check that 6 already exists in my set.
06:14It means that it exists in array 1. If array 1 and array 2 exist in array 2, then it
06:21will be common. No, it will not be common. So, I will take a count variable, which will count how
06:26much the intersection is. Now it is 0. For 6, I will check 6. After that, I will check 3.
06:32For 3, I will check 3. So, what will I do? I will increase the count.
06:38by one. Now, as I have increased the count, let's say 3 after 3, will I count increase by one?
06:47No. 1 element intersection can be one time, because there are unique elements in intersection. That's why if I have
06:543 once counted in intersection, then I have to delete this set of 3. That means if the next time
07:00it comes to 3, it will not count 2 times. That's why I have 3, I have to increase the
07:05count and delete the count.
07:08Now I will come to 9. What will 9 do? 9 will check. What exists? 9 will exist here. So
07:13I will increase the count to 2 and I will remove my set from 9. Then I will come to
07:192.
07:202 will check, it doesn't exist. Then I will come to 9. 9 will check, it doesn't exist. But if
07:26I have not deleted that 9, then my count will increase here, which is wrong. So after I increase the
07:32count every time, I have to delete my elements. So what do I do? My process is that first of
07:37all, I will check if exists. If exists exists, then I will do the count plus plus and remove.
07:50from the set of the element. If I don't do it, I will not do anything in that case. But
07:55if it exists, the count will increase and the element will remove. Next, I will come to 4. What will
08:014 do? 4 will check. 4 will not do exist. Basically, if I have a count, I will have a
08:05final value that is equal to 2. So, in the intersection of the elements, that will be equal to 2,
08:11which will be 3 and 9.
08:12So in this way we are going to write code. Union code will be the same as we have already
08:16written. It is very clear. In the intersection code, I will add 1 set of code in the intersection. Then
08:22I will loop on array 2. I will check if array 2 elements exist in my set. If they exist,
08:28I will do count plus plus and remove the element. So in this process,
08:34I will have n plus m time. In this process, I will have n plus m time. So, I will
08:38have two values, union and intersection, in a linear time complexity. I have given integer array 1 and integer array
08:472. I will create a hash set of integers. I will name set is equal to new hash set.
08:55First, let's calculate our union. What do I need to do for union? I want to insert all array's elements
09:01in my set. First, let's use array1 to i is equal to 0 from array1.length i++. I will add
09:11array1 to the i-th index element. After that, let's use another loop. for int i is equal to 0
09:19i less than array2.length i++.
09:23What will I do every time? I will insert all the array of second elements in set.add array to
09:31of my i. Last, what will I do? My union will be equal to the size of my set. So
09:38I will print my union equals my set's size. So I will call the .size method and print it. Next
09:47step is to calculate my intersection. Intersection.
09:52For the intersection, first of all, I need a empty set. So what can I do? I can clear the
09:56previous set. Because I have calculated the union, and I don't need it. So I have called the clear method
10:02to set. And now I'm going to reuse it. First of all, add the elements of the array 1. Then
10:09the work is to check all the elements of the array 2. So I can add the same loop to
10:17the array 1.
10:19Then I will run a loop for int i equals 0 i less than array 2 dot length i++. I
10:27will check every time that if the element exists in my set or array 2 of i exists in that
10:35case, what will I do in that case? In that case, I have my count variable which I have initialized
10:40with 0.
10:42I will do that count plus plus and remove my array to of icon so that it will not count
10:49twice and twice. Only one time count because in the intersection there will be a unique elements count. I will
10:55get the count from here. I will print my intersection is equal to here. I will do save and now
11:05run. I have given array which we have dry run.
11:12The union is equal to 6 and the intersection is equal to 2. Now we can print their elements as
11:17well. If you want to print any union elements, what will I do here? I will run a loop here,
11:25iterate and print all my unique elements in the union. If I want to print the intersection, I will remove
11:32this element as well. What will happen? My intersection elements will be printed. That is very easy to do. In
11:41fact, I will tell you
11:42that you have to write this code as a homework problem. In the homework problem you have to print all
11:47the elements in the homework problem and all the elements of the intersection.