Union of two arrays - 450DSA Cracker Solution
Union of two arrays
Given two arrays a[] and b[] of size n and m respectively. The task is to find the number of elements in the union between these two arrays.
Union of the two arrays can be defined as the set containing distinct elements from both the arrays. If there are repetitions, then only one occurrence of element should be printed in the union.
Note : Elements are not necessarily distinct.
Example 1:
Input:
5 3
1 2 3 4 5
1 2 3
Output:
5
Explanation:
1, 2, 3, 4 and 5 are the
elements which comes in the union set
of both arrays. So count is 5.
Example 2:
Input:
6 2
85 25 1 32 54 6
85 2
Output:
7
Explanation:
85, 25, 1, 32, 54, 6, and
2 are the elements which comes in the
union set of both arrays. So count is 7.
Code - using set (STL)
class Solution{
public:
//Function to return the count of number of elements in union of two arrays.
int doUnion(int a[], int n, int b[], int m) {
//code here
set<int> val;
for(int i=0;i<n;i++)
val.insert(a[i]);
for(int i=0;i<m;i++)
val.insert(b[i]);
return val.size();
}
};
Code - using Map(STL)
class Solution{
public:
//Function to return the count of number of elements in union of two arrays.
int doUnion(int a[], int n, int b[], int m) {
//code here
map<int, int> m1;
for(int i=0;i<n;i++)
m1[a[i]]++;
for(int i=0;i<m;i++)
m1[b[i]]++;
return m1.size();
}
};