C program to perform union operation on two arrays

The union operation on arrays combines all elements from both arrays while eliminating duplicates. This is similar to the mathematical set union operation, where each element appears only once in the result.

Syntax

int unionArrays(int arr1[], int size1, int arr2[], int size2, int result[]);

Union Operation Example

Given two arrays:

  • Array 1 = {1, 2, 3, 4, 6}
  • Array 2 = {1, 2, 5, 6, 7}

The union operation produces:

Array1 ? Array2 = {1, 2, 3, 4, 5, 6, 7}

Array 1 Array 2 Union Result {1, 2, 3, 4, 6} {1, 2, 5, 6, 7} {1, 2, 3, 4, 5, 6, 7}

Program

Following is the C program to perform union operation on two arrays −

#include <stdio.h>

int removeDuplicates(int size, int arr[]);
void sort(int size, int arr[]);

int main() {
    int arr1[] = {1, 2, 3, 4, 6};
    int arr2[] = {1, 2, 5, 6, 7};
    int size1 = 5;
    int size2 = 5;
    int uni[size1 + size2];
    int i, j = 0;
    
    printf("Array 1: ");
    for(i = 0; i < size1; i++) {
        printf("%d ", arr1[i]);
    }
    printf("<br>");
    
    printf("Array 2: ");
    for(i = 0; i < size2; i++) {
        printf("%d ", arr2[i]);
    }
    printf("<br>");
    
    /* Copy all elements from both arrays */
    for(i = 0; i < size1; i++) {
        uni[j] = arr1[i];
        j++;
    }
    for(i = 0; i < size2; i++) {
        uni[j] = arr2[i];
        j++;
    }
    
    /* Sort the union array */
    sort(size1 + size2, uni);
    
    /* Remove duplicate elements */
    int finalSize = removeDuplicates(size1 + size2, uni);
    
    printf("Union of arrays: ");
    for(i = 0; i < finalSize; i++) {
        printf("%d ", uni[i]);
    }
    printf("<br>");
    
    return 0;
}

int removeDuplicates(int size, int arr[]) {
    int i, j, k;
    for(i = 0; i < size; i++) {
        for(j = i + 1; j < size; ) {
            if(arr[i] == arr[j]) {
                for(k = j; k < size - 1; k++) {
                    arr[k] = arr[k + 1];
                }
                size--;
            } else {
                j++;
            }
        }
    }
    return size;
}

void sort(int size, int arr[]) {
    int i, j, temp;
    for(i = 0; i < size; i++) {
        for(j = i + 1; j < size; j++) {
            if(arr[i] > arr[j]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}

Output

Array 1: 1 2 3 4 6 
Array 2: 1 2 5 6 7 
Union of arrays: 1 2 3 4 5 6 7 

How It Works

  1. Concatenation: First, copy all elements from both arrays into a single array
  2. Sorting: Sort the combined array to group duplicate elements together
  3. Duplicate Removal: Remove consecutive duplicate elements by shifting remaining elements

Key Points

  • Union operation preserves all unique elements from both arrays
  • The result contains no duplicate elements
  • Time complexity is O(n²) due to sorting and duplicate removal
  • Space complexity is O(n? + n?) for the union array

Conclusion

The union operation combines two arrays by merging all unique elements. This implementation uses sorting and duplicate removal to ensure each element appears only once in the final result.

Updated on: 2026-03-15T14:08:32+05:30

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements