API调用工程师来了。。。采用流进行排序解决关键序列问题
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
String line = s.nextLine();
char[] chars = line.toCharArray();
HashMap<Character,Integer> map = new HashMap<>();
for (char aChar : chars) {
Integer integer = map.get(aChar);
if (null != integer){
map.put(aChar,integer+1);
continue;
}
map.put(aChar,1);
}
Set<Map.Entry<Character, Integer>> entries = map.entrySet();
List<Map.Entry<Character, Integer>> collect = entries.stream().sorted(Comparator.comparingInt(Map.Entry::getValue)).collect(Collectors.toList());
int less = 0;
for (Map.Entry<Character, Integer> entry : collect) {
if (less == 0) {
less = entry.getValue();
}
if (entry.getValue() > less){
break;
}
line= line.replace(String.valueOf(entry.getKey()), "");
}
System.out.println(line);
}
}