forked from mejtfk/Algorithms-HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopKFrequentElements.java
More file actions
30 lines (27 loc) · 939 Bytes
/
TopKFrequentElements.java
File metadata and controls
30 lines (27 loc) · 939 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
28
29
30
import java.util.*;
/**
* Program to find top k frequently present elements in the Input Sample
*
* @author neeraj on 07/10/19
* Copyright (c) 2019, Algorithms-HacktoberFest.
* All rights reserved.
*/
public class TopKFrequentElements {
public static void main(String[] args) {
System.out.println(topKFrequent(new int[]{1, 1, 1, 2, 2, 3}, 2));
System.out.println(topKFrequent(new int[]{1}, 1));
}
public static List<Integer> topKFrequent(int[] nums, int k) {
Map<Integer, Integer> freq = new HashMap<>();
for (int i : nums) {
freq.put(i, freq.getOrDefault(i, 0) + 1);
}
List<Integer> keys = new ArrayList<>(freq.keySet());
Collections.sort(keys, (a, b) -> freq.get(b) - freq.get(a));
List<Integer> result = new ArrayList<>();
for (int i = 0; i < k; i++) {
result.add(keys.get(i));
}
return result;
}
}