题解 | #汽水瓶#
删除字符串中出现次数最少的字符
http://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
```import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str=sc.nextLine();
HashMap<Character,Integer> map=new HashMap<>();
char tmp;
for (int i = 0; i <str.length() ; i++) {
//通过charAt(),进行获取字符串的值
tmp= str.charAt(i);
//判断这个tmp是否已经在map中
if (map.containsKey(tmp)){
//存在的话就用value+1
map.put(tmp,map.get(tmp)+1);
}else {
map.put(tmp,1);
}
}
int least = least(map);//获取了出现次数最小的数字
for (Character key : map.keySet()) {
if (map.get(key) == least){
str = str.replaceAll(Character.toString(key), "");
}
}
System.out.println(str);
sc.close();
}
//再写一个方法,计算Map的最小值
public static int least(HashMap<Character,Integer> map){
//获取map的值,放在数组中
HashSet<Integer> set = new HashSet<>();
//把出现的次数存到集合中
for (Character key:map.keySet()){
set.add(map.get(key));
}
return Collections.min(set);
}
}