# Make two sets disjoint by removing minimum elements

Given two sets of integers as two arrays of size m and n. Find count of minimum numbers that should be removed from the sets so that both set become disjoint or don’t contains any elements in common. We can remove elements from any set. We need to find minimum total elements to be removed.

Examples:

```Input : set1[] = {20, 21, 22}
set2[] = {22, 23, 24, 25}
Output : 1
We need to remove at least 1 element
which is 22 from set1 to make two
sets disjoint.

Input : set1[] = {20, 21, 22}
set2[] = {22, 23, 24, 25, 20}
Output : 2

Input : set1[] = {6, 7}
set2[] = {12, 13, 14, 15}
Output : 0
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

If we take a closer look at this problem, we can observe that the problem reduces to finding intersection of two sets.

A simple solution is iterate through every element of first set and for every element, check if it is present in second set. If present, increment count of elements to be removed.

```// C++ simple program to find total elements
// to be removed to make two sets disjoint.
#include<bits/stdc++.h>
using namespace std;

// Function for find minimum elements to be
// removed from both sets so that they become
// disjoint
int makeDisjoint(int set1[], int set2[], int n, int m)
{
int result = 0;
for (int i=0; i<n; i++)
{
int j;
for (j=0; j<m; j++)
if (set1[i] == set2[j])
break;

if (j != m)
result++;
}

return result;
}

// Driven code
int main()
{
int set1[] = {20, 21, 22};
int set2[] =  {22, 23, 24, 25, 20};
int n = sizeof(set1)/sizeof(set1[0]);
int m = sizeof(set2)/sizeof(set2[1]);
cout << makeDisjoint(set1, set2, n, m);
return 0;
}
```

Output:

```2
```

Time complexity : O(m * n)
Auxiliary Space : O(1)

An efficient solution is to use hashing. We create an empty hash and insert all items of set 1 in it. Now iterate through set 2 and for every element, check if it is present is hash. If present, increment count of elements to be removed.

```// C++ efficient program to find total elements
// to be removed to make two sets disjoint.
#include<bits/stdc++.h>
using namespace std;

// Function for find minimum elements to be
// removed from both sets so that they become
// disjoint
int makeDisjoint(int set1[], int set2[], int n, int m)
{
// Store all elements of first array in a hash
// table
unordered_set <int> s;
for (int i = 0; i < n; i++)
s.insert(set1[i]);

// We need to remove all those elements from
// set2 which are in set1.
int result = 0;
for (int i = 0;  i < m; i++)
if (s.find(set2[i]) != s.end())
result++;

return result;
}

// Driven code
int main()
{
int set1[] = {20, 21, 22};
int set2[] =  {22, 23, 24, 25, 20};
int n = sizeof(set1)/sizeof(set1[0]);
int m = sizeof(set2)/sizeof(set2[1]);
cout << makeDisjoint(set1, set2, n, m);
return 0;
}
```

Output:

```2
```

Time complexity : O(m + n)
Auxiliary Space : O(m)

This article is contributed by smarakchopdar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
1.1 Average Difficulty : 1.1/5.0
Based on 6 vote(s)