Given two unsorted arrays that represent two sets (elements in every array are distinct), find union and intersection of two arrays.

For example, if the input arrays are:

arr1[] = {7, 1, 5, 2, 3, 6}

arr2[] = {3, 8, 6, 20, 7}

Then your program should print Union as {1, 2, 3, 5, 6, 7, 8, 20} and Intersection as {3, 6}. Note that the elements of union and intersection can be printed in any order.

**Method 1 (Naive)**

**Union:**

1) Initialize union U as empty.

2) Copy all elements of first array to U.

3) Do following for every element x of second array:

…..a) If x is not present in first array, then copy x to U.

4) Return U.

**Intersection:**

1) Initialize intersection I as empty.

2) Do following for every element x of first array

…..a) If x is present in second array, then copy x to I.

4) Return I.

Time complexity of this method is O(mn) for both operations. Here m and n are number of elements in arr1[] and arr2[] respectively.

**Method 2 (Use Sorting)**

1) Sort arr1[] and arr2[]. This step takes O(mLogm + nLogn) time.

2) Use O(m + n) algorithms to find union and intersection of two sorted arrays.

Overall time complexity of this method is O(mLogm + nLogn).

**Method 3 (Use Sorting and Searching)**

**Union:**

1) Initialize union U as empty.

2) Find smaller of m and n and sort the smaller array.

3) Copy the smaller array to U.

4) For every element x of larger array, do following

…….b) Binary Search x in smaller array. If x is not present, then copy it to U.

5) Return U.

**Intersection:**

1) Initialize intersection I as empty.

2) Find smaller of m and n and sort the smaller array.

3) For every element x of larger array, do following

…….b) Binary Search x in smaller array. If x is present, then copy it to I.

4) Return I.

Time complexity of this method is min(mLogm + nLogm, mLogn + nLogn) which can also be written as O((m+n)Logm, (m+n)Logn). This approach works much better than the previous approach when difference between sizes of two arrays is significant.

Thanks to use_the_force for suggesting this method in a comment here.

Below is the implementation of this method.

## C

// A C++ program to print union and intersection /// of two unsorted arrays #include <iostream> #include <algorithm> using namespace std; int binarySearch(int arr[], int l, int r, int x); // Prints union of arr1[0..m-1] and arr2[0..n-1] void printUnion(int arr1[], int arr2[], int m, int n) { // Before finding union, make sure arr1[0..m-1] // is smaller if (m > n) { int *tempp = arr1; arr1 = arr2; arr2 = tempp; int temp = m; m = n; n = temp; } // Now arr1[] is smaller // Sort the first array and print its elements (these two // steps can be swapped as order in output is not important) sort(arr1, arr1 + m); for (int i=0; i<m; i++) cout << arr1[i] << " "; // Search every element of bigger array in smaller array // and print the element if not found for (int i=0; i<n; i++) if (binarySearch(arr1, 0, m-1, arr2[i]) == -1) cout << arr2[i] << " "; } // Prints intersection of arr1[0..m-1] and arr2[0..n-1] void printIntersection(int arr1[], int arr2[], int m, int n) { // Before finding intersection, make sure arr1[0..m-1] // is smaller if (m > n) { int *tempp = arr1; arr1 = arr2; arr2 = tempp; int temp = m; m = n; n = temp; } // Now arr1[] is smaller // Sort smaller array arr1[0..m-1] sort(arr1, arr1 + m); // Search every element of bigger array in smaller // array and print the element if found for (int i=0; i<n; i++) if (binarySearch(arr1, 0, m-1, arr2[i]) != -1) cout << arr2[i] << " "; } // A recursive binary search function. It returns // location of x in given array arr[l..r] is present, // otherwise -1 int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l)/2; // If the element is present at the middle itself if (arr[mid] == x) return mid; // If element is smaller than mid, then it can only // be presen in left subarray if (arr[mid] > x) return binarySearch(arr, l, mid-1, x); // Else the element can only be present in right subarray return binarySearch(arr, mid+1, r, x); } // We reach here when element is not present in array return -1; } /* Driver program to test above function */ int main() { int arr1[] = {7, 1, 5, 2, 3, 6}; int arr2[] = {3, 8, 6, 20, 7}; int m = sizeof(arr1)/sizeof(arr1[0]); int n = sizeof(arr2)/sizeof(arr2[0]); cout << "Union of two arrays is n"; printUnion(arr1, arr2, m, n); cout << "nIntersection of two arrays is n"; printIntersection(arr1, arr2, m, n); return 0; }

## Java

import java.util.Arrays; class UnionAndIntersection { // Prints union of arr1[0..m-1] and arr2[0..n-1] void printUnion(int arr1[], int arr2[], int m, int n) { // Before finding union, make sure arr1[0..m-1] // is smaller if (m > n) { int tempp[] = arr1; arr1 = arr2; arr2 = tempp; int temp = m; m = n; n = temp; } // Now arr1[] is smaller // Sort the first array and print its elements (these two // steps can be swapped as order in output is not important) Arrays.sort(arr1); for (int i = 0; i < m; i++) System.out.print(arr1[i] + " "); // Search every element of bigger array in smaller array // and print the element if not found for (int i = 0; i < n; i++) { if (binarySearch(arr1, 0, m - 1, arr2[i]) == -1) System.out.print(arr2[i] + " "); } } // Prints intersection of arr1[0..m-1] and arr2[0..n-1] void printIntersection(int arr1[], int arr2[], int m, int n) { // Before finding intersection, make sure arr1[0..m-1] // is smaller if (m > n) { int tempp[] = arr1; arr1 = arr2; arr2 = tempp; int temp = m; m = n; n = temp; } // Now arr1[] is smaller // Sort smaller array arr1[0..m-1] Arrays.sort(arr1); // Search every element of bigger array in smaller array // and print the element if found for (int i = 0; i < n; i++) { if (binarySearch(arr1, 0, m - 1, arr2[i]) != -1) System.out.print(arr2[i] + " "); } } // A recursive binary search function. It returns location of x in // given array arr[l..r] is present, otherwise -1 int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; // If the element is present at the middle itself if (arr[mid] == x) return mid; // If element is smaller than mid, then it can only // be present in left subarray if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); // Else the element can only be present in right subarray return binarySearch(arr, mid + 1, r, x); } // We reach here when element is not present in array return -1; } // Driver program to test above functions public static void main(String[] args) { UnionAndIntersection u_i = new UnionAndIntersection(); int arr1[] = {7, 1, 5, 2, 3, 6}; int arr2[] = {3, 8, 6, 20, 7}; int m = arr1.length; int n = arr2.length; System.out.println("Union of two arrays is "); u_i.printUnion(arr1, arr2, m, n); System.out.println(""); System.out.println("Intersection of two arrays is "); u_i.printIntersection(arr1, arr2, m, n); } }

Output:

Union of two arrays is 3 6 7 8 20 1 5 2 Intersection of two arrays is 7 3 6

**Method 4 (Use Hashing)**

**Union:**

**Union**

1. Initialize an empty hash set **hs**.

2. Iterate through the first array and put every element of the first array in the set S.

3. Repeat the process for the second array.

4. Print the set **hs**.

**Intersection**

1. Initialize an empty set hs.

2. Iterate through the first array and put every element of the first array in the set S.

3. For every element x of the second array, do the following :

Search x in the set hs. If x is present, then print it.

Time complexity of this method is Θ(m+n) under the assumption that hash table search and insert operations take Θ(1) time.

## C++

// CPP program to find union and intersection // using sets #include <bits/stdc++.h> using namespace std; // Prints union of arr1[0..n1-1] and arr2[0..n2-1] void printUnion(int arr1[], int arr2[], int n1, int n2) { set<int> hs; // Inhsert the elements of arr1[] to set hs for (int i = 0; i < n1; i++) hs.insert(arr1[i]); // Insert the elements of arr2[] to set hs for (int i = 0; i < n2; i++) hs.insert(arr2[i]); // Print the content of set hs for (auto it = hs.begin(); it != hs.end(); it++) cout << *it << " "; cout << endl; } // Prints intersection of arr1[0..n1-1] and // arr2[0..n2-1] void printIntersection(int arr1[], int arr2[], int n1, int n2) { set<int> hs; // Insert the elements of arr1[] to set S for (int i = 0; i < n1; i++) hs.insert(arr1[i]); for (int i = 0; i < n2; i++) // If element is present in set then // push it to vector V if (hs.find(arr2[i]) != hs.end()) cout << arr2[i] << " "; } // Driver Program int main() { int arr1[] = { 7, 1, 5, 2, 3, 6 }; int arr2[] = { 3, 8, 6, 20, 7 }; int n1 = sizeof(arr1) / sizeof(arr1[0]); int n2 = sizeof(arr2) / sizeof(arr2[0]); printUnion(arr1, arr2, n1, n2); printIntersection(arr1, arr2, n1, n2); return 0; }

## Java

// Java program to find union and intersection // using Hashing import java.util.HashSet; class Test { // Prints union of arr1[0..m-1] and arr2[0..n-1] static void printUnion(int arr1[], int arr2[]) { HashSet<Integer> hs = new HashSet<>(); for (int i = 0; i < arr1.length; i++) hs.add(arr1[i]); for (int i = 0; i < arr2.length; i++) hs.add(arr2[i]); System.out.println(hs); } // Prints intersection of arr1[0..m-1] and arr2[0..n-1] static void printIntersection(int arr1[], int arr2[]) { HashSet<Integer> hs = new HashSet<>(); HashSet<Integer> hs1 = new HashSet<>(); for (int i = 0; i < arr1.length; i++) hs.add(arr1[i]); for (int i = 0; i < arr2.length; i++) if (hs.contains(arr2[i])) System.out.print(arr2[i] + " "); } // Driver method to test the above function public static void main(String[] args) { int arr1[] = {7, 1, 5, 2, 3, 6}; int arr2[] = {3, 8, 6, 20, 7}; System.out.println("Union of two arrays is : "); printUnion(arr1, arr2); System.out.println("Intersection of two arrays is : "); printIntersection(arr1, arr2); } }

This method is contributed by

**Ankur Singh**.

### Asked in: Rockstand

See following post for sorted arrays.

Find Union and Intersection of two sorted arrays

Please write comments if you find any bug in above codes/algorithms, or find other ways to solve the same problem.