-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnion of Arrays with Duplicates.cpp
75 lines (58 loc) · 1.49 KB
/
Union of Arrays with Duplicates.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//{ Driver Code Starts
// Initial template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function template in C++
class Solution {
public:
// Function to return the count of number of elements in union of two arrays.
int findUnion(vector<int>& a, vector<int>& b) {
// code here
int n1 = a.size();
int n2 = b.size();
unordered_map<int, int> hashh;
int i = 0;
while(i < n1){
hashh[a[i]]++;
i++;
}
i = 0;
while(i < n2) {
hashh[b[i]]++;
i++;
}
int count = 0;
for(auto it : hashh) count++;
return count;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
cin.ignore(); // Ignore the newline character after reading t
while (t--) {
vector<int> a;
vector<int> b;
string input;
// For a
getline(cin, input); // Read the entire line for the array elements
stringstream ss(input);
int number;
while (ss >> number) {
a.push_back(number);
}
// For b
getline(cin, input); // Read the entire line for the array elements
stringstream ss2(input);
while (ss2 >> number) {
b.push_back(number);
}
Solution ob;
cout << ob.findUnion(a, b) << endl;
cout << '~' << endl;
}
return 0;
}
// } Driver Code Ends