-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathUnion_Arrays.cpp
50 lines (38 loc) · 1.1 KB
/
Union_Arrays.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
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 considered in the union.
The final count should be printed.
*/
#include <bits/stdc++.h>
using namespace std;
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) {
set<int>s;
for (int i = 0; i < n; i++)
s.insert(a[i]);
for (int i = 0; i < m; i++)
s.insert(b[i]); //duplicates will not be added, property of sets
return s.size();
}
};
int main() {
//enter number of test cases.
int t;
cin >> t;
while(t--){
int n, m;
//accept size of both the arrays
cin >> n >> m;
int a[n], b[m];
//accept the arrays
for(int i = 0;i<n;i++)
cin >> a[i];
for(int i = 0;i<m;i++)
cin >> b[i];
Solution ob;
cout << ob.doUnion(a, n, b, m) << endl; //count is displayed
}
return 0;
}