Union and Intersection of two sorted arrays

Given two sorted arrays, find their union and intersection.

Example:

```Input:
arr1[] = {1, 3, 4, 5, 7}
arr2[] = {2, 3, 5, 6}
Output:
Union : {1, 2, 3, 4, 5, 6, 7}
Intersection : {3, 5}```

We strongly recommend that you click here and practice it, before moving on to the solution.

Algorithm Union(arr1[], arr2[])

For union of two arrays, follow the following merge procedure.
1) Use two index variables i and j, initial values i = 0, j = 0
2) If arr1[i] is smaller than arr2[j] then print arr1[i] and increment i.
3) If arr1[i] is greater than arr2[j] then print arr2[j] and increment j.
4) If both are same then print any of them and increment both i and j.
5) Print remaining elements of the larger array.

C

```// C program to find union of
// two sorted arrays
#include<stdio.h>

/* Function prints union of arr1[] and arr2[]
m is the number of elements in arr1[]
n is the number of elements in arr2[] */
int printUnion(int arr1[], int arr2[], int m, int n)
{
int i = 0, j = 0;
while (i < m && j < n)
{
if (arr1[i] < arr2[j])
printf(" %d ", arr1[i++]);
else if (arr2[j] < arr1[i])
printf(" %d ", arr2[j++]);
else
{
printf(" %d ", arr2[j++]);
i++;
}
}

/* Print remaining elements of the larger array */
while(i < m)
printf(" %d ", arr1[i++]);
while(j < n)
printf(" %d ", arr2[j++]);
}

/* Driver program to test above function */
int main()
{
int arr1[] = {1, 2, 4, 5, 6};
int arr2[] = {2, 3, 5, 7};
int m = sizeof(arr1)/sizeof(arr1[0]);
int n = sizeof(arr2)/sizeof(arr2[0]);
printUnion(arr1, arr2, m, n);
getchar();
return 0;
}
```

Java

```// Java program to find union of
// two sorted arrays

class FindUnion
{
/* Function prints union of arr1[] and arr2[]
m is the number of elements in arr1[]
n is the number of elements in arr2[] */
static int printUnion(int arr1[], int arr2[], int m, int n)
{
int i = 0, j = 0;
while (i < m && j < n)
{
if (arr1[i] < arr2[j])
System.out.print(arr1[i++]+" ");
else if (arr2[j] < arr1[i])
System.out.print(arr2[j++]+" ");
else
{
System.out.print(arr2[j++]+" ");
i++;
}
}

/* Print remaining elements of
the larger array */
while(i < m)
System.out.print(arr1[i++]+" ");
while(j < n)
System.out.print(arr2[j++]+" ");

return 0;
}

public static void main(String args[])
{
int arr1[] = {1, 2, 4, 5, 6};
int arr2[] = {2, 3, 5, 7};
int m = arr1.length;
int n = arr2.length;
printUnion(arr1, arr2, m, n);
}
}
```

Python

```# Python program to find union of
# two sorted arrays
# Function prints union of arr1[] and arr2[]
# m is the number of elements in arr1[]
# n is the number of elements in arr2[]
def printUnion(arr1, arr2, m, n):
i,j = 0,0
while i < m and j < n:
if arr1[i] < arr2[j]:
print(arr1[i])
i += 1
elif arr2[j] < arr1[i]:
print(arr2[j])
j+= 1
else:
print(arr2[j])
j += 1
i += 1

# Print remaining elements of the larger array
while i < m:
print(arr1[i])
i += 1

while j < n:
print(arr2[j])
j += 1

# Driver program to test above function
arr1 = [1, 2, 4, 5, 6]
arr2 = [2, 3, 5, 7]
m = len(arr1)
n = len(arr2)
printUnion(arr1, arr2, m, n)

# This code is contributed by Pratik Chhajer
```

Output:

```1 2 3 4 5 6 7
```

Time Complexity: O(m+n)

Algorithm Intersection(arr1[], arr2[])

For Intersection of two arrays, print the element only if the element is present in both arrays.
1) Use two index variables i and j, initial values i = 0, j = 0
2) If arr1[i] is smaller than arr2[j] then increment i.
3) If arr1[i] is greater than arr2[j] then increment j.
4) If both are same then print any of them and increment both i and j.

C

```// C program to find intersection of
// two sorted arrays
#include<stdio.h>

/* Function prints Intersection of arr1[] and arr2[]
m is the number of elements in arr1[]
n is the number of elements in arr2[] */
int printIntersection(int arr1[], int arr2[], int m, int n)
{
int i = 0, j = 0;
while (i < m && j < n)
{
if (arr1[i] < arr2[j])
i++;
else if (arr2[j] < arr1[i])
j++;
else /* if arr1[i] == arr2[j] */
{
printf(" %d ", arr2[j++]);
i++;
}
}
}

/* Driver program to test above function */
int main()
{
int arr1[] = {1, 2, 4, 5, 6};
int arr2[] = {2, 3, 5, 7};
int m = sizeof(arr1)/sizeof(arr1[0]);
int n = sizeof(arr2)/sizeof(arr2[0]);
printIntersection(arr1, arr2, m, n);
getchar();
return 0;
}
```

Java

```// Java program to find intersection of
// two sorted arrays

class FindIntersection
{
/* Function prints Intersection of arr1[] and arr2[]
m is the number of elements in arr1[]
n is the number of elements in arr2[] */
static void printIntersection(int arr1[], int arr2[], int m, int n)
{
int i = 0, j = 0;
while (i < m && j < n)
{
if (arr1[i] < arr2[j])
i++;
else if (arr2[j] < arr1[i])
j++;
else
{
System.out.print(arr2[j++]+" ");
i++;
}
}
}

public static void main(String args[])
{
int arr1[] = {1, 2, 4, 5, 6};
int arr2[] = {2, 3, 5, 7};
int m = arr1.length;
int n = arr2.length;
printIntersection(arr1, arr2, m, n);
}
}
```

Python

```# Python program to find intersection of
# two sorted arrays
# Function prints Intersection of arr1[] and arr2[]
# m is the number of elements in arr1[]
# n is the number of elements in arr2[]
def printIntersection(arr1, arr2, m, n):
i,j = 0,0
while i < m and j < n:
if arr1[i] < arr2[j]:
i += 1
elif arr2[j] < arr1[i]:
j+= 1
else:
print(arr2[j])
j += 1
i += 1

# Driver program to test above function
arr1 = [1, 2, 4, 5, 6]
arr2 = [2, 3, 5, 7]
m = len(arr1)
n = len(arr2)
printIntersection(arr1, arr2, m, n)

# This code is contributed by Pratik Chhajer
```

Output:

```2 5
```

Time Complexity: O(m+n)

Another approach that is useful when difference between sizes of two given arrays is significant.

The idea is to iterate through the shorter array and do a binary search for every element of short array in big array (note that arrays are sorted). Time complexity of this solution is O(min(mLogn, nLogm)). This solution works better than the above approach when ratio of larger length to smaller is more than logarithmic order.

See following post for unsorted arrays.
Find Union and Intersection of two unsorted arrays

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

GATE CS Corner    Company Wise Coding Practice

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