Union operation on arrays combines the elements of two or more arrays into a single array. All the elements on the resultant array will be unique without repetition.
This article deals with the implementation of union operation on unsorted arrays. (Click here if you wish to view the C version).
Union of arrays in C++ |
The following header file is used -
- iostream - For input or output stream.
- displayed - This function is used to display the array. This function receives an array pointer and the size of the array as parameters and does not return any value.
- unions - This function is used to perform union operations on arrays. This function receives 3 array pointers of which union operation is performed on two arrays and the result is stored in the 3rd array and 2 integer values which are the lengths of the arrays on which the union operation is performed. This function returns the length of the resultant array.
//Set operation - Union on Arrays by Vishruth Codes
#include<iostream>
using namespace std;
void displayed(int a[], int n)
{
for(int i=0;i<n;i++)
{
if(i==n-1)
{
cout<<a[i]<<".";
}
else{
cout<<a[i]<<", ";
}
}
}
int unions(int a[], int n, int b[], int m, int res[])
{
int i=0,k=0,t=0,count=0;
//Copying the elements of first array into the result array.
while(i<n)
{
res[t++]=a[i++];
}
while(i<m+n && k<m)
{
int j=0;
while(j<i+1)
{
if (res[j]==b[k])
{
k++;
//the value of i should be updated only is the adjacent space is occupied.
//Here, adjacent space is not occupied, hence to prevent jump to next space
//we write i--
i--;
break;
}
else if(j==i)
{
res[j]=b[k];
k++;
count++;
break;
}
else{
j++;
}
}
i++;
}
return (n+count);
}
int main()
{
int n,m;
int ar1[]={1,4,9,16,32};
int ar2[]={1,2,3,4,5,6,7,8,9,10};
n=sizeof(ar1)/sizeof(ar1[0]);
cout<<"\nArray1: ";
displayed(ar1,n);
m=sizeof(ar2)/sizeof(ar2[0]);
cout<<"\nArray2: ";
displayed(ar2,m);
int un[n+m];
int size=unions(ar1,n,ar2,m,un);
//Here size is the length of union
cout<<"\nThe union: ";
displayed(un,size);
return 0;
}
Subject -
In the above example, I have implemented a C++ program to perform union operation on unsorted arrays.
Basic features -
- This program performs union operation on any two unsorted arrays.
- The arrays on which the union operation is performed are hardcoded into the program.
- In this program, the two unsorted arrays(ar1,ar2) [on which the union operation is performed] are hardcoded into the program.
- The lengths of the two arrays(ar1,ar2) are calculated and stored(n,m) and the elements of the arrays are printed separately via the displayed function.
- In order to perform the union operation, we create another array(un) to store the result. Since we don't know the result yet, we assume the worst case (i.e there are no common elements in both the arrays) and keep its size as the sum of the lengths of both the arrays.
- We pass the 2 arrays(ar1,ar2) upon which the union operation is to be performed along with their sizes and the 3rd array created to store their result into the unions function.
- The unions function copies all the elements of the first array (ar1) into the resultant array and then compares each element of the second array(ar2) with all the inserted elements of the resultant array, and only if the element is completely unique, it is inserted into the resultant array at the position adjacent to the previously inserted element. A count element is also maintained to count the no. of elements of the second array(ar2) inserted into the resultant array.
Visualization of working of unions function - Then the length of the resultant array is calculated, which is the sum of lengths of the first array(ar1) and count. This value is returned. (This is done to only display the elements which are the result of union operation, and not garbage value. See point 3 for clear understanding).
- Since the arrays are passed by address, the changes made to the formal parameters in the function will be reflected in the actual parameters i.e. will be reflected on the resultant array un.
- The resultant array and its length are passed on to the displayed function to display the result.
Output -
Sample Output |
Thanks for reading. If you found any errors or have suggestions to improve the article, please mention them in the comments.
0 Comments