I wanted to apply brute force to find the union of two arrays and theoretically it should works but for some reason only first array goes into 3rd array(3rd array is for storing elements form array 1 and array2) and size of 3rd array is getting increased from 8 to 12
//find the union of two arrays
#include<iostream>
#include<vector>
using namespace std;
void uniarr(vector<int> &arr1, vector<int> &arr2)
{
int n=arr1.size();
int m=arr2.size();
vector<int> arr3(n+m);
cout<<" arr "<<arr3.size()<<endl;
int count=1;
for (int i = 0; i < n; i++)
{ count=1;
for (int j = 0; j < arr2.size(); j++)
{
if(arr1[i]==arr2[j])
{
if(count==1)
{
arr3.push_back(arr1[i]);
count++;
}
arr2.erase(arr2.begin()+j);
j=j-1;
}
}
if(count==1)
{
arr3.push_back(arr1[i]);
}
}
cout<<" arr "<<arr3.size()<<endl;
for (int i = 0; i < arr3.size(); i++)
{
cout<<" "<<arr3.at(i);
}
}
int main()
{
system("cls");
vector<int> arr1={3,1,4,6};
vector<int> arr2={1,2,5,4};
uniarr(arr1,arr2);
return 0;
}