-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTopKFrequent.java
More file actions
27 lines (25 loc) · 810 Bytes
/
Copy pathTopKFrequent.java
File metadata and controls
27 lines (25 loc) · 810 Bytes
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
import java.util.Arrays;
import java.util.Map;
import java.util.List;
import static java.util.Comparator.reverseOrder;
import static java.util.Map.Entry.comparingByValue;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;
public class TopKFrequent {
public static void main(String[] args) {
System.out.println(topKFrequent(new int[]{1, 1, 1, 1, 2, 2, 2, 3, 3, 4}, 3));
}
public static List<Integer> topKFrequent(int[] nums, int k) {
return Arrays
.stream(nums)
.boxed()
.collect(groupingBy(x -> x, counting()))
.entrySet()
.stream()
.sorted(comparingByValue(reverseOrder()))
.limit(k)
.map(Map.Entry::getKey)
.collect(toList());
}
}