C Program To Find The Union of Two Arrays

C Program To Find The Union of Two Arrays:
To get the union of two arrays, follow these steps:
Push the first array in a HashSet instance.
Use the addAll() method to add the elements of the second array into the set.

C Program To Find The Union of Two Arrays

#include<iostream>
using namespace std;
int main()
{
  int n1,n2,i,j,flag;
  cout<<"Enter the no. of elements of the 1st array: ";
  cin>>n1;
  int arr1[n1];
  cout<<"\nEnter the elements of the 1st array: ";
  for(i=0;i<n1;i++)
  {
    cin>>arr1[i];
  }
  cout<<"\nEnter the no. of elements of the 2nd array: ";
  cin>>n2;
  int arr2[n2];
  cout<<"\nEnter the elements of the 2nd array: ";
  for(i=0;i<n2;i++)
  {
    cin>>arr2[i];
  }
  cout<<"\nUnion of the two arrays: ";
  for(i=0;i<n1;i++)
  {
    cout<<arr1[i]<<" ";
  }

  for(j=0;j<n2;j++)
  {
    flag=0;
    for(i=0;i<n1;i++)
    {
      if(arr1[i]==arr2[j])
      {
        flag=1;
        break;
      }
    }
    if(flag!=1)
    {
      cout<<arr2[j]<<" ";
    }
    
  }
  
  return 0;
}

Output of Program

Enter the no. of elements of the 1st array: 5

Enter the elements of the 1st array: 2
1
23
32
24

Enter the no. of elements of the 2nd array: 5

Enter the elements of the 2nd array: 35
25
253
53
53

Union of the two arrays: 2 1 23 32 24 35 25 253 53 53