- What is the union of two arrays
- How to find the union of two arrays
- How to handle duplicates in union
- Step-by-step explanation of the program
- Practice exercises to test your understanding
Introduction
In this tutorial, we will learn how to write a C program to find the union of two arrays.
The union of two arrays is a set of all unique elements from both arrays combined. It is used in many real-world applications, such as:
- Combining two lists of contacts without duplicates
- Merging two product catalogs
- Creating a complete list of keywords from multiple documents
- Combining two datasets for analysis
💡 Key Point: The union of two arrays contains all unique elements from both arrays. Unlike intersection, which only keeps common elements, union includes all elements from both arrays without duplicates.
C Program for Union of Two Arrays
#include <stdio.h>
int main() {
int n1, n2, i, j, k;
int count = 0;
// ====== FIRST ARRAY ======
printf("Enter the number of elements in first array: ");
scanf("%d", &n1);
int arr1[n1];
printf("Enter %d elements:\n", n1);
for(i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}
// ====== SECOND ARRAY ======
printf("\nEnter the number of elements in second array: ");
scanf("%d", &n2);
int arr2[n2];
printf("Enter %d elements:\n", n2);
for(i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}
// ====== FIND UNION ======
// Maximum possible size is n1 + n2
int union_array[n1 + n2];
// Copy all elements from first array
for(i = 0; i < n1; i++) {
union_array[count] = arr1[i];
count++;
}
// Add elements from second array that are not already in union
for(i = 0; i < n2; i++) {
int duplicate = 0;
for(j = 0; j < count; j++) {
if(arr2[i] == union_array[j]) {
duplicate = 1;
break;
}
}
if(duplicate == 0) {
union_array[count] = arr2[i];
count++;
}
}
// ====== DISPLAY ======
printf("\nFirst array: ");
for(i = 0; i < n1; i++) {
printf("%d ", arr1[i]);
}
printf("\nSecond array: ");
for(i = 0; i < n2; i++) {
printf("%d ", arr2[i]);
}
printf("\n\nUnion of two arrays: ");
for(i = 0; i < count; i++) {
printf("%d ", union_array[i]);
}
printf("\n");
return 0;
}
Sample Output
Enter the number of elements in first array: 5 Enter 5 elements: 10 20 30 40 50 Enter the number of elements in second array: 4 Enter 4 elements: 30 50 60 70 First array: 10 20 30 40 50 Second array: 30 50 60 70 Union of two arrays: 10 20 30 40 50 60 70
Example with Duplicates:
Enter the number of elements in first array: 6 Enter 6 elements: 10 20 10 30 20 40 Enter the number of elements in second array: 5 Enter 5 elements: 10 10 20 50 60 First array: 10 20 10 30 20 40 Second array: 10 10 20 50 60 Union of two arrays: 10 20 30 40 50 60
Example with All Unique Elements:
Enter the number of elements in first array: 3 Enter 3 elements: 10 20 30 Enter the number of elements in second array: 3 Enter 3 elements: 40 50 60 First array: 10 20 30 Second array: 40 50 60 Union of two arrays: 10 20 30 40 50 60
Program Explanation
Let's break down the code step by step:
- Include Header File:
#include <stdio.h>includes the standard input/output library. - Declare Variables:
int n1, n2;— sizes of the two arraysint i, j, k;— loop countersint count = 0;— number of elements in the union
- Read Arrays: Reads the size and elements of both arrays.
- Find Union:
- Step 1: Copy all elements from the first array into the union array
- Step 2: For each element in the second array, check if it already exists in the union
- Step 3: If not a duplicate, add it to the union array
- Display Results: Prints both arrays and the union.
- Return:
return 0;indicates successful program execution.
📝 Note: The union array size is n1 + n2, which is the maximum possible size. The actual size of the union is stored in the count variable.
Algorithm to Find Union of Two Arrays
Step-by-step algorithm:
- Start
- Read the elements of first array (arr1)
- Read the elements of second array (arr2)
- Create an empty union array
- Copy all elements from arr1 to union array
- For each element in arr2:
- Check if the element already exists in the union array
- If not present, add it to the union array
- Print the union array
- End
Union of Sorted Arrays (More Efficient)
If both arrays are sorted, we can find the union in O(n1 + n2) time using a two-pointer approach.
#include <stdio.h>
int main() {
int n1, n2, i, j, k;
int count = 0;
printf("Enter the number of elements in first sorted array: ");
scanf("%d", &n1);
int arr1[n1];
printf("Enter %d sorted elements:\n", n1);
for(i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}
printf("\nEnter the number of elements in second sorted array: ");
scanf("%d", &n2);
int arr2[n2];
printf("Enter %d sorted elements:\n", n2);
for(i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}
// Find union using two-pointer technique
int union_array[n1 + n2];
i = 0;
j = 0;
while(i < n1 && j < n2) {
if(arr1[i] < arr2[j]) {
// Add arr1[i] if not already added
if(count == 0 || union_array[count-1] != arr1[i]) {
union_array[count] = arr1[i];
count++;
}
i++;
}
else if(arr1[i] > arr2[j]) {
// Add arr2[j] if not already added
if(count == 0 || union_array[count-1] != arr2[j]) {
union_array[count] = arr2[j];
count++;
}
j++;
}
else {
// Equal elements - add once
if(count == 0 || union_array[count-1] != arr1[i]) {
union_array[count] = arr1[i];
count++;
}
i++;
j++;
}
}
// Add remaining elements from first array
while(i < n1) {
if(count == 0 || union_array[count-1] != arr1[i]) {
union_array[count] = arr1[i];
count++;
}
i++;
}
// Add remaining elements from second array
while(j < n2) {
if(count == 0 || union_array[count-1] != arr2[j]) {
union_array[count] = arr2[j];
count++;
}
j++;
}
// Display results
printf("\nFirst array: ");
for(i = 0; i < n1; i++) {
printf("%d ", arr1[i]);
}
printf("\nSecond array: ");
for(i = 0; i < n2; i++) {
printf("%d ", arr2[i]);
}
printf("\n\nUnion of two arrays: ");
for(i = 0; i < count; i++) {
printf("%d ", union_array[i]);
}
printf("\n");
return 0;
}
Sample Output:
Enter the number of elements in first sorted array: 6 Enter 6 sorted elements: 10 20 20 30 40 50 Enter the number of elements in second sorted array: 5 Enter 5 sorted elements: 20 30 50 60 70 First array: 10 20 20 30 40 50 Second array: 20 30 50 60 70 Union of two arrays: 10 20 30 40 50 60 70
How Two-Pointer Union Works
- Two Pointers:
ipoints to arr1,jpoints to arr2 - Compare: If
arr1[i] < arr2[j], add arr1[i] and movei - If
arr1[i] > arr2[j], add arr2[j] and movej - If
arr1[i] == arr2[j], add the element once and move both pointers - Handle Duplicates: Check if the last added element is the same before adding
- Time Complexity: O(n1 + n2)
Intersection vs Union
| Feature | Intersection | Union |
|---|---|---|
| Definition | Common elements | All unique elements |
| Example | A={1,2,3}, B={2,3,4} → {2,3} | A={1,2,3}, B={2,3,4} → {1,2,3,4} |
| Size | ≤ min(n1, n2) | ≤ n1 + n2 |
| Use Case | Find common items | Combine lists without duplicates |
Time and Space Complexity
| Method | Time Complexity | Space Complexity |
|---|---|---|
| Nested Loop (Unsorted) | O(n1 × n2) | O(n1 + n2) |
| Two-Pointer (Sorted) | O(n1 + n2) | O(n1 + n2) |
💻 Practice Exercise
Challenge 1: Find the union of three arrays.
Challenge 2: Find the union of two arrays without using extra space (modify one of the arrays).
🔍 Click to Show Solution for Challenge 2
#include <stdio.h>
int main() {
int n1, n2, i, j;
printf("Enter the number of elements in first array: ");
scanf("%d", &n1);
int arr1[n1];
printf("Enter %d elements:\n", n1);
for(i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}
printf("Enter the number of elements in second array: ");
scanf("%d", &n2);
int arr2[n2];
printf("Enter %d elements:\n", n2);
for(i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}
// Copy all elements from arr1 to result (using arr1 itself)
int result_size = n1;
// Add elements from arr2 that are not in arr1
for(i = 0; i < n2; i++) {
int duplicate = 0;
for(j = 0; j < result_size; j++) {
if(arr2[i] == arr1[j]) {
duplicate = 1;
break;
}
}
if(duplicate == 0) {
arr1[result_size] = arr2[i];
result_size++;
}
}
printf("\nUnion of two arrays: ");
for(i = 0; i < result_size; i++) {
printf("%d ", arr1[i]);
}
printf("\n");
return 0;
}
Frequently Asked Questions
1. What is the union of two arrays?
The union of two arrays is a set of all unique elements that appear in either array. It combines both arrays and removes duplicates.
2. How do you find the union of two arrays in C?
Copy all elements from the first array into the union. Then, for each element in the second array, check if it already exists in the union. If not, add it.
3. What is the difference between union and intersection?
Union contains all unique elements from both arrays. Intersection contains only the elements that appear in both arrays.
4. How do you handle duplicates in union?
Before adding an element to the union, check if it already exists in the union array. If it does, skip it. This ensures all elements are unique.
5. Can I find union without using extra space?
Yes, you can store the union in one of the original arrays by first copying all elements from the other array and then removing duplicates.
💡 Tip: If both arrays are sorted, use the two-pointer approach for a more efficient O(n1 + n2) solution.