笔试题 如何统计一个字符串里面的字符出现的次多的最多的数字?

import java.util.HashMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        String str = "abcdefgaaabbb";
        int max = 0;
        Map<Character, Integer> map = new HashMap<Character, Integer>(str.length());
        for (char c : str.toCharArray()) {
            Integer i = map.get(c);
            int value = (i == null) ? 0 : i;
            map.put(c, ++value);
            max = value > max ? value : max;
        }
        System.out.println(max);
    }
}

运行结果

``` 4 ```

相关文章:

  • 2022-12-23
  • 2021-11-29
  • 2021-05-26
  • 2022-12-23
  • 2021-12-05
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-10
相关资源
相似解决方案