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)

Fazal Rehman Shamil Click Here to Know More
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325
Dear Professors and Resarchers!You are welome to Cite these tutorials in your research or slides etc. Please don't forget to mention the reference of website. Copy Paste of text is strcitly forbidden. Images can be reuse because images are protected with watermark.

Leave a Reply

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