Java
哈希+排序
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Map<String,Integer> hash=new HashMap<>();
while(in.hasNext()){
String s=in.next();
hash.put(s,hash.getOrDefault(s,0)+1);
}
List<My> list=new ArrayList<>();
Set<String> key=hash.keySet();
for(String k:key){
Integer v=hash.get(k);
if(v>=3){
My my=new My(k,v);
list.add(my);
}
}
Collections.sort(list,(o1,o2)->{
if(o1.count!=o2.count){
return o2.count-o1.count;
}else{
return o1.s.compareTo(o2.s);
}
});
for(My my:list){
System.out.println(my.s);
}
}
}
class My{
String s;
Integer count;
public My(String s,Integer count){
this.s=s;
this.count=count;
}
}

京公网安备 11010502036488号