Learn Programming and technology with ease @ developerpublish.com

HomeC ProgrammingC Program to Find Union and Intersection of Two Arrays

# C Program to Find Union and Intersection of Two Arrays

This C program finds the union and intersection of two arrays. It takes two arrays as input and computes the resulting union and intersection arrays.

## Program Statement

Hereâ€™s the program statement for the C program to find the union and intersection of two arrays:

Program Statement:

Write a C program to find the union and intersection of two arrays. The program should take two input arrays `arr1` and `arr2` of integers, along with their sizes `n1` and `n2` respectively. The program should then find and print the union of the two arrays, followed by the intersection of the two arrays.

Input:

• Two input arrays `arr1` and `arr2` of integers.
• The size of `arr1`, denoted by `n1`.
• The size of `arr2`, denoted by `n2`.

Output:

• The union of `arr1` and `arr2`, without any duplicates.
• The intersection of `arr1` and `arr2`, containing only the common elements.

## C Program to Find Union and Intersection of Two Arrays

```#include <stdio.h>

void findUnion(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++;
}
}

while (i < m)
printf("%d ", arr1[i++]);

while (j < n)
printf("%d ", arr2[j++]);
}

void findIntersection(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 {
printf("%d ", arr2[j++]);
i++;
}
}
}

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]);

printf("Union: ");
findUnion(arr1, arr2, m, n);

printf("\nIntersection: ");
findIntersection(arr1, arr2, m, n);

return 0;
}
```

## How it works

Let me explain how the program works step by step:

1. In the `main` function, two arrays `arr1` and `arr2` are defined, along with their respective sizes `n1` and `n2`. The example arrays are:cssCopy code`arr1 = [1, 2, 4, 5, 6] arr2 = [2, 3, 5, 7]`
2. The `findUnion` function is called with `arr1`, `n1`, `arr2`, and `n2` as arguments. This function finds and prints the union of the two arrays.
3. Inside the `findUnion` function, two variables `i` and `j` are initialized to 0. These variables are used as indices to iterate over `arr1` and `arr2` respectively.
4. The function enters a loop that continues as long as `i` is less than `n1` (the size of `arr1`) and `j` is less than `n2` (the size of `arr2`).
5. Within the loop, the function compares the elements of `arr1[i]` and `arr2[j]`:
• If `arr1[i]` is less than `arr2[j]`, it means `arr1[i]` is not present in `arr2`. So, the element `arr1[i]` is printed as part of the union, and `i` is incremented to move to the next element in `arr1`.
• If `arr2[j]` is less than `arr1[i]`, it means `arr2[j]` is not present in `arr1`. So, the element `arr2[j]` is printed as part of the union, and `j` is incremented to move to the next element in `arr2`.
• If `arr1[i]` is equal to `arr2[j]`, it means the element is common to both arrays. So, the element is printed as part of the union, and both `i` and `j` are incremented to move to the next elements in both arrays.
6. After the loop ends, there may be some remaining elements in either `arr1` or `arr2`. The function uses two separate while loops to print any remaining elements in `arr1` and `arr2`, if any.
7. The `findIntersection` function is called with the same arguments as the `findUnion` function. This function finds and prints the intersection of the two arrays.
8. Inside the `findIntersection` function, a similar comparison loop is used to iterate over `arr1` and `arr2`. However, instead of printing elements, the function only increments `i` and `j` when the elements are not equal. When the elements are equal, it means the element is common to both arrays, so it is printed as part of the intersection.
9. Finally, the `main` function prints the output by calling the `printf` function. The union is printed first by calling `findUnion`, followed by the intersection, which is printed by calling `findIntersection`.

Thatâ€™s how the program works! It compares the elements of the two arrays and determines the union and intersection based on the comparison results.

## Input/Output

Share:

### You May Also Like

#### C Program to Find Volume and Surface Area of Sphere

This C program calculates the volume and surface area of a sphere using its radius. A sphere is a three-dimensional...

#### C Program to Convert Roman Number to Decimal Number

This C program converts a Roman numeral to a decimal number. Roman numerals are a system of numerical notation used...

#### C Program to Calculate the Value of sin(x)

This C program calculates the value of sin(x) using the Taylor series expansion. The Taylor series expansion is a mathematical...