题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://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(); //新建map集合统计每个字符出现的次数 Map<Character,Integer> map=new HashMap<>(); char[] chars = str.toCharArray(); for (char ch : chars) { map.put(ch,map.getOrDefault(ch,0)+1); } //计算最小出现次数 Collection<Integer> values = map.values(); Integer min = Collections.min(values); //遍历集合,删除出现最小次数的字符 Set<Map.Entry<Character,Integer>> set=map.entrySet(); Iterator<Map.Entry<Character, Integer>> it = set.iterator(); while (it.hasNext()){ Map.Entry<Character, Integer> next = it.next(); Character key=next.getKey(); Integer value = next.getValue(); if (value==min){ str = str.replaceAll(key.toString(), ""); } } System.out.println(str); } }