先抛个:        String s = "adfoweyirlkbasgxalueralsdhg"; // 待统计字符串
        int max = 0; // 记录最大出现次数
        int[] cnt = new int[127]; // 临时计数用的数组
        for (int i = 0; i < s.length(); i++) { // 循环字符以做统计
              char c = s.charAt(i); // 取出单个字母
              max = (++cnt[c] > max) ? cnt[c] : max; // 计数并检测最大出现次数
        }
        System.out.println (max);

解决方案 »

  1.   

    new一个数组,长度为26(或52,区分大小写),初始值为0
    遍历字符串每个字符,对应位置++。
    最后遍历数组,找出最大值。我是来打酱油滴
      

  2.   

    没看懂cnt[c]的意思啊,请指教啊
      

  3.   

    想明白了,原来数组的索引可以用字符写哦,对应的是ASCII值,大神果然NB
      

  4.   

    这个方法已经很给力了,效率非常高,唯一有点小瑕疵的就是数组空间有点浪费,127是提供给常用字符的,如果只是字母的话,就有点浪费。但是这个性能的消耗很小,无所谓了!我在这边用HashMap实现一个吧,思路和1楼的差不多,只是能适用各种字符,各种数组,效率也蛮高的,不过效率上肯定没有1楼的高!(hashMap最快也要在数组索引上面,hash一把,和equal一把,所以效率肯定低于数组索引)
    楼主参考参考:)
            String str = "jdsalfjaslnzdfuawoejlljafd";
            int max = 0;
            Map<Character, Integer> map = new HashMap<Character, Integer>(str.length());
            for(char chr : str.toCharArray()) {
                Integer i = map.get(chr);   
                int value = (i == null) ? 0 : i;  // 获取,没有则0,有则叠加
                map.put(chr, ++value);
                max = value > max ? value : max;  // 更新max
            }
            System.out.println(max);
      

  5.   

    ....
    他只要字母,所以我的数组长度是26或52。char和int可以转换吧?我只要做个判断,加减,就能让所有字母对应到我的数组的每一个位置。
    1楼的方法,如果字符串里的标点符号最多呢?也算吗?
      

  6.   

    上代码吧
    String str = "afdbsodfusdfalgaufaowejhnlgjaouowejhgajgabcdefghijklmnopqrstuvwxyz,./?'????????????????????????????????????????";
    int[] sum = new int[52];
    int max = 0;
    for (int i = 0; i < str.length(); i++) {
    char c = str.charAt(i);
    if (c >= 65 && c <= 90)//A~Z
    sum[c - 65]++;
    else if (c >= 97 && c <= 122)//a~z
    sum[c - 71]++;
    else
    continue;
    }
    for (int i = 0; i < sum.length; i++)
    if (sum[i] > max)
    max = sum[i];
    System.out.println(max);
    我的只不过是代码不好看。复杂度不见得高啊。
      

  7.   

    对楼主算法优化下(可以显示最多的那个字母,这里只考虑了26个小写字母,其中了浪费了cnt[0]):
    String s = "adfoweyyyzyyyyirlkbasgxalueralsdhg"; // 待统计字符串
            int max = 0; // 记录最大出现次数
            int[] cnt = new int[27]; // 临时计数用的数组
            char temp=' ';
            for (int i = 0; i < s.length(); i++) { // 循环字符以做统计
                  char c = s.charAt(i); // 取出单个字母
                  if(++cnt[c-96] > max){
                   max=cnt[c-96];
                   temp=c;
                  }else{
                   max=max;
                  }
            }
            System.out.println (max+" "+temp);
    }
      

  8.   

    修改:
    String s = "adfowzzzzzzzeyyyzyyyyirlkbasgxalueralsdhg"; // 待统计字符串
            int max = 0; // 记录最大出现次数
            int[] cnt = new int[26]; // 临时计数用的数组
            char temp=' ';
            for (int i = 0; i < s.length(); i++) { // 循环字符以做统计
                  char c = s.charAt(i); // 取出单个字母
                  if(++cnt[c-97] > max){
                   max=cnt[c-97];
                   temp=c;
                  }else{
                   max=max;
                  }
            }
            System.out.println (max+" "+temp);
    }
      

  9.   

    我面试的时候碰到这个问题,首先题意得理解一下,题中括号内特别强调:只要求找出出现最多字母的次数,不需要找出具体字母。
    这句话的意思是,如字符串“abadefdgad'中出现最多(3次)的字母应该是a和d,既然a和d出现最多,那么应该是2次,而不是他们单独出现的次数3次。
    否则题目应该表达为:找出字符串中字母出现次数最多的字母次数。这样才体现了这道题的深度,我给出的答案
    String s = "dagdkangangdewadhfadgjkdaghdia";
    char[] cs = s.toCharArray();
    int max_num = 1;// 次数
    int max = 0;// 出现最多的字母
    for (int i = 0; i < cs.length; i++) {
    int num = 0;// 每个字母出现次数
    if(i != 0 && s.substring(0, i).contains(s.substring(i, i+1))){
    continue;
    }
    for (int j = 0; j < cs.length; j++) {
    if(cs[i] == cs[j])
    num++;
    }
    if(num > max){
    max = num;
    max_num = 1;
    }else if(num == max){
    max_num++;
    }
    }
    System.out.println(max_num);