Repository navigation
Expand file tree
/
Copy pathLeetCode2248.java
More file actions
47 lines (41 loc) · 1.52 KB
/
Copy pathLeetCode2248.java
File metadata and controls
47 lines (41 loc) · 1.52 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
41
42
43
44
45
46
47
//2248. Intersection of Multiple Arrays
import java.util.*;
import java.util.stream.Collectors;
public class LeetCode2248 {
public List<Integer> intersection(int[][] nums) {
return Arrays.stream(nums)
.flatMap(arr -> Arrays.stream(arr).boxed())
.collect(Collectors.groupingBy(n -> n, Collectors.counting()))
.entrySet()
.stream()
.filter(e -> e.getValue() == nums.length)
//.sorted(Comparator.comparingInt(Map.Entry::getKey)) //28 ms
.map(Map.Entry::getKey)
.sorted() //25ms
.toList();
}
//21 ms
public List<Integer> intersection_2(int[][] nums) {
return Arrays.stream(nums)
.flatMapToInt(Arrays::stream)
.boxed()
.collect(Collectors.toMap( n-> n, n-> 1, Integer::sum,TreeMap::new))
.entrySet()
.stream()
.filter(e -> e.getValue() == nums.length)
.map(Map.Entry::getKey)
.toList();
}
//20ms
public List<Integer> intersection_3(int[][] nums) {
return Arrays.stream(nums)
.flatMap(arr -> Arrays.stream(arr).boxed())
.collect(Collectors.groupingBy(n -> n, Collectors.summingInt(e->1)))
.entrySet()
.stream()
.filter(e -> e.getValue() == nums.length)
.map(Map.Entry::getKey)
.sorted()
.toList();
}
}