Skip to content

Instantly share code, notes, and snippets.

@janvichhabra
Last active October 21, 2021 11:16
Show Gist options
  • Save janvichhabra/d86f9bd1b015cde179454f4d00d16899 to your computer and use it in GitHub Desktop.
Save janvichhabra/d86f9bd1b015cde179454f4d00d16899 to your computer and use it in GitHub Desktop.
pepcoding.com 21/10/21 getCommonElements-2
#include <iostream>
#include <unordered_map>
using namespace std;
int main(){
int n,m;
cin >> n;
int arr1[n];
unordered_map<int,int> mp;
for(int i=0;i<n;i++){
cin >> arr1[i];
mp[arr1[i]]++;
}
cin >> m;
int arr2[m];
for(int i=0;i<m;i++){
cin >> arr2[i];
if(mp.find(arr2[i])!=mp.end() && mp[arr2[i]]!=0){
cout << arr2[i] << endl;
mp[arr2[i]]--;
}
}
}
def main():
n1 = int(input())
arr1=[]
for i in range(n1):
arr1.append(int(input()))
n2 = int(input())
arr2=[]
for i in range(n2):
arr2.append(int(input()))
hm={}
for val in arr1:
if val in hm:
hm[val]=hm.get(val)+1
else:
hm[val]=1
for val in arr2:
if val in hm and hm.get(val)>0:
print(val)
hm[val]=hm.get(val)-1
if __name__ == '__main__':
main()
#include<bits/stdc++.h>
using namespace std;
int main(){
//write your code here
}
def main():
n1 = int(input())
arr1=[]
for i in range(n1):
arr1.append(int(input()))
n2 = int(input())
arr2=[]
for i in range(n2):
arr2.append(int(input()))
#write your code here
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment