C/C++ Program for Union of Two Arrays

Here you will get C/C++ program to find union of two arrays.

For example:

First array: {1, 3, 7, 9}

Second array: {1, 4, 6}

Union: {1, 3, 4, 7, 6, 9}

 

C/C++ Program for Union of Two Arrays

Union of Two Sorted Arrays

If two arrays are sorted then their union can be found in following way.

C Program

#include<stdio.h>

int main()
{
	int a1[20],a2[20],u[40],i,j,k,n,m;
	
	printf("Enter size of first array:");
	scanf("%d",&n);
	printf("Enter elements of first array in ascending order:\n");
	for(i=0;i<n;++i){
		scanf("%d",&a1[i]);
	}
	
	printf("\nEnter size of second array:");
	scanf("%d",&m);
	printf("Enter elements of second array in ascending order:\n");
	for(i=0;i<m;++i){
		scanf("%d",&a2[i]);
	}
	
	for(i=0,j=0,k=0;i<n&&j<m;){
		if(a1[i]<a2[j]){
			u[k]=a1[i];
			i++;
			k++;
		}
		else if(a1[i]>a2[j]){
			u[k]=a2[j];
			j++;
			k++;
		}
		else{
			u[k]=a1[i];
			i++;
			j++;
			k++;
		}
	}
	
	if(i<n){
		for(;i<n;++i){
			u[k]=a1[i];
			k++;
		}
	}
	else if(j<m){
		for(;j<m;++j){
			u[k]=a2[j];
			k++;
		}
	}
	
	printf("\nUnion of two arrays is:\n");
	for(i=0;i<k;++i){
		printf("%d ",u[i]);
	}

    return 0;
}

 

Output

Enter size of first array:4
Enter elements of first array in ascending order:
1 2 3 5

Enter size of second array:5
Enter elements of second array in ascending order:
1 3 5 7 9

Union of two arrays is:
1 2 3 5 7 9

 

C++ Program

#include<iostream>

using namespace std;

int main()
{
	int a1[20],a2[20],u[40],i,j,k,n,m;
	
	cout<<"Enter size of first array:";
	cin>>n;
	cout<<"Enter elements of first array in ascending order:\n";
	for(i=0;i<n;++i){
		cin>>a1[i];
	}
	
	cout<<"\nEnter size of second array:";
	cin>>m;
	cout<<"Enter elements of second array in ascending order:\n";
	for(i=0;i<m;++i){
		cin>>a2[i];
	}
	
	for(i=0,j=0,k=0;i<n&&j<m;){
		if(a1[i]<a2[j]){
			u[k]=a1[i];
			i++;
			k++;
		}
		else if(a1[i]>a2[j]){
			u[k]=a2[j];
			j++;
			k++;
		}
		else{
			u[k]=a1[i];
			i++;
			j++;
			k++;
		}
	}
	
	if(i<n){
		for(;i<n;++i){
			u[k]=a1[i];
			k++;
		}
	}
	else if(j<m){
		for(;j<m;++j){
			u[k]=a2[j];
			k++;
		}
	}
	
	cout<<"\nUnion of two arrays is:\n";
	for(i=0;i<k;++i){
		cout<<u[i]<<" ";
	}

    return 0;
}

 

Union of Two Unsorted Arrays

If two arrays are unsorted then their union can be found in following way.

C Program

#include<stdio.h>

int main()
{
	int a1[20],a2[20],u[40],i,j,k,n,m,flag;
	
	printf("Enter size of first array:");
	scanf("%d",&n);
	printf("Enter elements of first array:\n");
	for(i=0;i<n;++i){
		scanf("%d",&a1[i]);
	}
	
	printf("\nEnter size of second array:");
	scanf("%d",&m);
	printf("Enter elements of second array:\n");
	for(i=0;i<m;++i){
		scanf("%d",&a2[i]);
	}
	
	k=0;
	for(i=0;i<n;++i){
		u[k]=a1[i];
		k++;
	}
	
	for(i=0;i<m;++i){
		flag=1;
		for(j=0;j<n;++j){
			if(a2[i]==a1[j]){
				flag=0;
				break;
			}
		}
		
		if(flag){
			u[k]=a2[i];
			k++;
		}
	}
	
	printf("\nUnion of two arrays is:\n");
	for(i=0;i<k;++i){
		printf("%d ",u[i]);
	}

    return 0;
}

 

Output

Enter size of first array:3
Enter elements of first array:
1 3 5

Enter size of second array:4
Enter elements of second array:
1 2 3 6

Union of two arrays is:
1 3 5 2 6

 

C++ Program

#include<iostream>

using namespace std;

int main()
{
	int a1[20],a2[20],u[40],i,j,k,n,m,flag;
	
	cout<<"Enter size of first array:";
	cin>>n;
	cout<<"Enter elements of first array in ascending order:\n";
	for(i=0;i<n;++i){
		cin>>a1[i];
	}
	
	cout<<"\nEnter size of second array:";
	cin>>m;
	cout<<"Enter elements of second array in ascending order:\n";
	for(i=0;i<m;++i){
		cin>>a2[i];
	}
	
		k=0;
	for(i=0;i<n;++i){
		u[k]=a1[i];
		k++;
	}
	
	for(i=0;i<m;++i){
		flag=1;
		for(j=0;j<n;++j){
			if(a2[i]==a1[j]){
				flag=0;
				break;
			}
		}
		
		if(flag){
			u[k]=a2[i];
			k++;
		}
	}

	cout<<"\nUnion of two arrays is:\n";
	for(i=0;i<k;++i){
		cout<<u[i]<<" ";
	}

    return 0;
}

4 thoughts on “C/C++ Program for Union of Two Arrays”

  1. I’m sorry sir but sometimes can you puta comment on your program, it really helps if you put some code thanks. And followed you and added (y)

  2. sir what if the array elemnts have more than 1 elemnts repeating
    like if array 1 has 1,2,3,3,4,4,4,5 and array 2 has 7,8,9,9,9
    will this code work ????

Leave a Comment

Your email address will not be published. Required fields are marked *