Skip to content
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
darshangouda
leetcode-java-streams
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
leetcode-java-streams
/
src
/
LeetCode692.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
聽
History
History
History
21 lines (20 loc) 路 739 Bytes
main
Breadcrumbs
leetcode-java-streams
/
src
/
LeetCode692.java
Copy path
Top
File metadata and controls
Code
Blame
21 lines (20 loc) 路 739 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
//692. Top K Frequent Words
public class LeetCode692 {
public List<String> topKFrequent(String[] words, int k) {
return Arrays.stream(words)
.collect(Collectors.groupingBy(str->str,Collectors.counting()))
.entrySet()
.stream()
.sorted(Comparator.<Map.Entry<String,Long>>comparingLong(Map.Entry::getValue)
.reversed()
.thenComparing(Map.Entry::getKey,Comparator.naturalOrder()))
.limit(k)
.map(Map.Entry::getKey)
.toList();
}
}
You can鈥檛 perform that action at this time.