# 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++

 `// C++ simple program to find total elements ` `// to be removed to make two sets disjoint. ` `#include ` `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

## Java

 `// Java simple program to find  ` `// total elements to be removed ` `// to make two sets disjoint. ` `import` `java.io.*; ` ` `  `public` `class` `GFG{ ` ` `  `// Function for find minimum elements  ` `// to be removed from both sets ` `// so that they become disjoint ` `static` `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; ` `} ` ` `  `    ``// Driver code ` `    ``static` `public` `void` `main (String[] args) ` `    ``{ ` `    ``int` `[]set1 = {``20``, ``21``, ``22``}; ` `    ``int` `[]set2 = {``22``, ``23``, ``24``, ``25``, ``20``}; ` `    ``int` `n = set1.length; ` `    ``int` `m = set2.length; ` `    ``System.out.println(makeDisjoint(set1, set2, ` `                                    ``n, m)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## Python 3

 `# Python 3 simple program to find  ` `# total elements to be removed to  ` `# make two sets disjoint. ` ` `  `# Function for find minimum elements  ` `# to be removed from both sets so that  ` `# they become disjoint ` `def` `makeDisjoint(set1, set2, n, m): ` ` `  `    ``result ``=` `0``; ` `    ``for` `i ``in` `range` `(``0``, n ``-` `1``): ` `        ``for` `j ``in` `range``(``0``, m ``-` `1``): ` `            ``if` `(set1[i] ``=``=` `set2[j]): ` `                ``break` ` `  `        ``if` `(j !``=` `m): ` `            ``result ``+``=` `1` ` `  `    ``return` `result ` ` `  `# Driver Code ` `set1 ``=` `[``20``, ``21``, ``22``] ` `set2 ``=` `[``22``, ``23``, ``24``, ``25``, ``20``] ` `n ``=` `len``(set1) ` `m ``=` `len``(set2) ` `print``(makeDisjoint(set1, set2, n, m)) ` ` `  `# This code is contributed  ` `# by Akanksha Rai `

## C#

 `// C# simple program to find ` `// total elements to be removed ` `// to make two sets disjoint. ` `using` `System; ` ` `  `public` `class` `GFG{ ` ` `  `// Function for find minimum elements  ` `// to be removed from both sets ` `// so that they become disjoint ` `static` `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; ` `} ` ` `  `    ``// Driver code ` `    ``static` `public` `void` `Main () ` `    ``{ ` `    ``int` `[]set1 = {20, 21, 22}; ` `    ``int` `[]set2 = {22, 23, 24, 25, 20}; ` `    ``int` `n = set1.Length; ` `    ``int` `m = set2.Length; ` `    ``Console.WriteLine(makeDisjoint(set1, set2, ` `                                   ``n, m)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

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++

 `// C++ efficient program to find total elements ` `// to be removed to make two sets disjoint. ` `#include ` `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; ` `} ` ` `  `// Driver 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; ` `} `

## Java

 `// Java efficient program to find total elements ` `// to be removed to make two sets disjoint. ` `import` `java.util.*; ` `class` `GFG { ` ` `  `// Function for find minimum elements to be ` `// removed from both sets so that they become ` `// disjoint ` `    ``static` `int` `makeDisjoint(``int` `set1[], ``int` `set2[], ``int` `n, ``int` `m) { ` `        ``// Store all elements of first array in a hash ` `        ``// table ` `        ``LinkedHashSet s = ``new` `LinkedHashSet(); ` `        ``for` `(``int` `i = ``0``; i < n; i++) { ` `            ``s.add(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.contains(set2[i])) { ` `                ``result++; ` `            ``} ` `        ``} ` ` `  `        ``return` `result; ` `    ``} ` ` `  `// Driver code ` `    ``public` `static` `void` `main(String[] args) { ` `        ``int` `set1[] = {``20``, ``21``, ``22``}; ` `        ``int` `set2[] = {``22``, ``23``, ``24``, ``25``, ``20``}; ` `        ``int` `n = set1.length; ` `        ``int` `m = set2.length; ` `        ``System.out.println(makeDisjoint(set1, set2, n, m)); ` `    ``} ` `} ` `// This code is contributed by PrinciRaj1992 `

## Python 3

 `# Python 3 efficient program to find  ` `# total elements to be removed to  ` `# make two sets disjoint. ` ` `  `# Function for find minimum elements  ` `# to be removed from both sets so  ` `# that they become disjoint ` `def` `makeDisjoint(set1, set2, n, m): ` ` `  `    ``# Store all elements of first array  ` `    ``# in a hash table ` `    ``s ``=` `[] ` `    ``for` `i ``in` `range``(n): ` `        ``s.append(set1[i]) ` `         `  `    ``# We need to remove all those elements  ` `    ``# from set2 which are in set1. ` `    ``result ``=` `0` `    ``for` `i ``in` `range``(m): ` `        ``if` `set2[i] ``in` `s: ` `            ``result ``+``=` `1` ` `  `    ``return` `result ` ` `  `# Driver code ` `if` `__name__ ``=``=``"__main__"``: ` ` `  `    ``set1 ``=` `[``20``, ``21``, ``22``] ` `    ``set2 ``=` `[``22``, ``23``, ``24``, ``25``, ``20``] ` `    ``n ``=` `len``(set1) ` `    ``m ``=` `len``(set2) ` `    ``print``(makeDisjoint(set1, set2, n, m)) ` ` `  `# This code is contributed  ` `# by ChitraNayal `

## C#

 `// C# efficient program to find total elements ` `// to be removed to make two sets disjoint. ` `using` `System; ` `using` `System.Collections.Generic;              ` ` `  `class` `GFG ` `{ ` ` `  `    ``// Function for find minimum elements to be ` `    ``// removed from both sets so that they become ` `    ``// disjoint ` `    ``static` `int` `makeDisjoint(``int` `[]set1, ``int` `[]set2,  ` `                            ``int` `n, ``int` `m)  ` `    ``{ ` `        ``// Store all elements of first array in a hash ` `        ``// table ` `        ``HashSet<``int``> s = ``new` `HashSet<``int``>(); ` `        ``for` `(``int` `i = 0; i < n; i++)  ` `        ``{ ` `            ``s.Add(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.Contains(set2[i]))  ` `            ``{ ` `                ``result++; ` `            ``} ` `        ``} ` `        ``return` `result; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args)  ` `    ``{ ` `        ``int` `[]set1 = {20, 21, 22}; ` `        ``int` `[]set2 = {22, 23, 24, 25, 20}; ` `        ``int` `n = set1.Length; ` `        ``int` `m = set2.Length; ` `        ``Console.WriteLine(makeDisjoint(set1, set2, n, m)); ` `    ``} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.