-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComparision_to_sorting.cpp
39 lines (37 loc) · 1021 Bytes
/
Comparision_to_sorting.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
//Our task is to calculate the number of elements that belong
//44
//to both of the lists. For example, for the lists
//A = [5,2,8,9,4] and B = [3,2,9,5],
//the answer is 3 because the numbers 2, 5 and 9 belong to both of the lists.
#include <iostream>
#include <unordered_set>
using namespace std;
int main(int argc, const char * argv[]) {
unordered_set<int> s,s1;
int input , n , m , count=0;
cout<<"Enter size of array A\n";
cin>>n;
cout<<"Enter size of array B\n";
cin>>m;
cout<<"Enter array A elements\n";
for(int i=0 ; i<n ; i++){
cin>>input;
s.insert(input);
}
cout<<"Enter array B elements\n";
for(int i=0 ; i<m ; i++){
cin>>input;
s1.insert(input);
}
for(int i : s)
cout<<i<<" ";
cout<<endl;
for(auto x : s){
if(s1.count(x))
count++;
}//algorithm 1; O(nlogn) complexity using set
//algorithm 2; O(n) complexity using unordered_set
//
cout<<count<<"\n";
return 0;
}