forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(GeeksforGeeks) Intersection_of_arrays.cpp
53 lines (45 loc) · 1.08 KB
/
(GeeksforGeeks) Intersection_of_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
51
52
53
// { Driver Code Starts
//Initial template for C++
#include <bits/stdc++.h>
using namespace std;
int NumberofElementsInIntersection(int a[], int b[], int n, int m);
// } Driver Code Ends
//User function template for C++
// Given two arrays A and B and their sizes N and M respectively
int NumberofElementsInIntersection(int a[], int b[], int n, int m)
{
// Your code goes here
int countCommon = 0, index;
map<int, int> mp;
for (index = 0; index < n; index++)
{
mp[a[index]]++;
}
for (index = 0; index < m; index++)
{
if (mp.find(b[index]) != mp.end())
{
countCommon++;
mp.erase(b[index]);
}
}
return countCommon;
}
// { Driver Code Starts.
int main()
{
int t;
cin >> t;
while (t--)
{
int n, m;
cin >> n >> m;
int a[n], b[m];
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < m; i++)
cin >> b[i];
cout << NumberofElementsInIntersection(a, b, n, m) << endl;
}
return 0;
} // } Driver Code Ends