题解 | #删除字符串中出现次数最少的字符#

删除字符串中出现次数最少的字符

https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9

import java.util.Scanner;
import java.util.HashMap;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String nextLine = scanner.nextLine();

        HashMap<Character, Integer> hashMap = new HashMap<>();
        char[] toCharArray = nextLine.toCharArray();

        for (char aChar : toCharArray) {
            if (hashMap.containsKey(aChar)) {
                hashMap.put(aChar, hashMap.get(aChar) + 1);
            } else {
                hashMap.put(aChar, 1);
            }
        }

        int min = Integer.MAX_VALUE;
        for (Integer value : hashMap.values()) {
            min = Math.min(min, value);
        }

        for (char aChar : toCharArray) {
            if (hashMap.get(aChar) != min) {
                System.out.print(aChar);
            }
        }
        System.out.println();
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务