forked from CodeSadhu/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetCommon1.java
39 lines (34 loc) · 1010 Bytes
/
GetCommon1.java
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
package HashMap;
import java.util.HashMap;
import java.util.Scanner;
public class GetCommon1 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a = s.nextInt();
int [] arr = new int[a];
for (int i = 0; i < arr.length ; i++) {
arr[i] = s.nextInt();
}
int b = s.nextInt();
int [] arr2 = new int[b];
for (int i = 0; i < arr.length ; i++) {
arr2[i] = s.nextInt();
}
HashMap<Integer,Integer> hm = new HashMap<>();
for(int val : arr){
if(hm.containsKey(val)){
int of = hm.get(val);
int nf = of + 1 ;
hm.put(val , nf);
}else{
hm.put(val, 1);
}
}
for(int val : arr2){
if(hm.containsKey(val)){
System.out.println(val);
hm.remove(val);
}
}
}
}