The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

Union of Array

Union of Array

Here your task is to ask user to enter any elements in a set of array and you have to extract all elements in both array without duplicating any elements.

C Program - Union of Array

Let us write a C program to extract all elements in both arrays without duplicating any elements.

c-union-array.c
#include <stdio.h>
int main()
{
int a[10], b[10], flag = 0, n1, n2, i, j;
printf("Enter array1 size : ");
scanf("%d",&n1);
printf("\nEnter array2 size : ");
scanf("%d",&n2);
printf("\nEnter array1 element : ");
for(i = 0;i < n1;i++)
scanf("%d",&a[i]);
printf("\nEnter array2 element : ");
for(i = 0;i < n2;i++)
scanf("%d",&b[i]);
printf("\nUnion:");
for(i = 0;i < n1;i++)
printf("%d, ",a[i]);
for(i = 0;i < n2;i++)
{
for(j = 0;j < n1;j++)
{
if(b[i] == a[j])
{
flag = 1;
}
}
if(flag == 0)
{
printf("%d, ",b[i]);
}
flag = 0;
}
return 0;
}
Enter array1 size : 5
Enter array2 size : 3
Enter array1 element : 1 2 3 4 5
Enter array2 element : 5 6 7
Union : 1, 2, 3, 4, 5, 6, 7

Note:

Here we have two arrays, we compare every individual elements of one array(b[]) to elements in another array(a[]) and print all the non matching elements.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report