题面
核心思想
map记录String的出现次数
set去重+自定义排序
代码
import java.util.*;
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
final long MOD = (long) (1e9 + 7);
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
String[] s1 = s.split(" ");
HashMap<String,Integer> v= new HashMap<>();
//统计次数
for(String ss: s1){
v.put(ss, v.getOrDefault(ss, 0) + 1);
}
TreeSet<String> st = new TreeSet<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int cnt1 = v.get(o1);
int cnt2 = v.get(o2);
if(cnt1 != cnt2) {
return cnt2 - cnt1; // 按次数从大到小排序
}else {
return o1.compareTo(o2);// 字典升序
}
}
});
for(String ss: s1){
if(v.get(ss) >= 3) {
st.add(ss);
}
}
for(String ss: st)
System.out.println(ss);
}
}
标签:24,String,小红书,ss,int,cnt2,秋招,new
From: https://www.cnblogs.com/ganyq/p/18119206