-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCommon in 3 Sorted Arrays.java
More file actions
40 lines (32 loc) · 1.12 KB
/
Common in 3 Sorted Arrays.java
File metadata and controls
40 lines (32 loc) · 1.12 KB
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
class Solution {
//donot edit this code
// Function to find common elements in three arrays.
public List<Integer> commonElements(List<Integer> arr1, List<Integer> arr2,
List<Integer> arr3) {
Set<Integer> hashSet = new HashSet<>(arr1);
Set<Integer> hashSet1 = new HashSet<>(arr2);
Set<Integer> hashSet2 = new HashSet<>(arr3);
HashMap<Integer, Integer> map = new HashMap<>();
ArrayList<Integer> arrayList = new ArrayList<>();
for (Integer i : hashSet) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
for (Integer i : hashSet1) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
for (Integer i : hashSet2) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() >= 3) {
arrayList.add(entry.getKey());
}
}
if (arrayList.isEmpty() ) {
arrayList.add(-1);
return arrayList;
}
Collections.sort(arrayList);
return arrayList;
}
}