Program to find the union of two unsorted arrays in CPP (C plus plus)

Program to find the union of two unsorted arrays in CPP (C plus plus)

In this tutorial, we will learn about the Program to find the union of two unsorted arrays in CPP (C plus plus).

#include<iostream>
 
using namespace std;
 
int main()
{
    
	int i;
	int j;
	int l;
	int no;
	int m;
	int array[20];
	int array1[20];
	int array2[40];
	int flag;
    
    cout<<"Please enter the size of first array: ";
    cin>>no;
    
    cout<<"Please enter the values of first array in ascending order: "<<endl;
    
    for(i=0;i<no;++i){
        cin>>array[i];
    }
    
    cout<<endl<<"Please enter the size of second array: ";
    cin>>m;
    
    cout<<"Please enter the values of second array in ascending order: "<<endl;
    
    for(i=0;i<m;++i){
        cin>>array1[i];
    }
    
        l=0;
    for(i=0;i<no;++i){
        array2[l]=array[i];
        l++;
    }
    
    for(i=0;i<m;++i){
        flag=1;
        for(j=0;j<no;++j){
            if(array1[i]==array[j]){
                flag=0;
                break;
            }
        }
        
        if(flag){
            array2[l]=array1[i];
            l++;
        }
    }
 
    cout<<endl<<"The union of two unsorted arrays is: ";
    
    for(i=0;i<l;++i){
        cout<<array2[i]<<" ";
    }
 
    return 0;
}

Output:

Program to find the union of two unsorted arrays in CPP (C plus plus)
Program to find the union of two unsorted arrays in CPP (C plus plus)
Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.