Skip to content

Commit b89a89e

Browse files
Add files via upload
1 parent 9120f2f commit b89a89e

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

DSA 450 GFG/Union_Arrays.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution{
5+
public:
6+
//Function to return the count of number of elements in union of two arrays.
7+
int doUnion(int a[], int n, int b[], int m) {
8+
set<int>s;
9+
for (int i = 0; i < n; i++)
10+
s.insert(a[i]);
11+
12+
for (int i = 0; i < m; i++)
13+
s.insert(b[i]); //duplicates will not be added, property of sets
14+
15+
return s.size();
16+
}
17+
};
18+
19+
20+
int main() {
21+
//enter number of test cases.
22+
int t;
23+
cin >> t;
24+
25+
while(t--){
26+
27+
int n, m;
28+
//accept size of both the arrays
29+
cin >> n >> m;
30+
int a[n], b[m];
31+
32+
//accept the arrays
33+
for(int i = 0;i<n;i++)
34+
cin >> a[i];
35+
36+
for(int i = 0;i<m;i++)
37+
cin >> b[i];
38+
39+
Solution ob;
40+
cout << ob.doUnion(a, n, b, m) << endl; //count is displayed
41+
42+
}
43+
44+
return 0;
45+
}

0 commit comments

Comments
 (0)