请教各路大神,这种方法是可以求出一个字符串中出现次数最多的字符个次数,但是如果有两个或多个不同字符出现的次数是一样且比其他字符多的时候,怎么把这些字符输出呢

解决方案 »

  1.   


    public static void main(String[] args) {
    System.out.println(doCount("aadsaccdc"));
    }

    public static List<Character> doCount(String string){
    List<Character> chars = new ArrayList<Character>();
    char[] cs = string.toCharArray();
    int max = 0;
    List<Character> find = new ArrayList<Character>();
    for(char c : cs){
    int count = 0;
    boolean isFind = false;
    for(Character fc : find){
    if(c == fc.charValue()){
    isFind = true;
    break;
    }
    }
    if(!isFind){
    find.add(c);
    Matcher m = Pattern.compile(String.valueOf(c)).matcher(string);
    while(m.find()){
    count++;
    }
    if(count == max){
    chars.add(c);
    }else if(count > max){
    max = count;
    chars.clear();
    chars.add(c);
    }
    }
    }
    return chars;
    }
      

  2.   

    把max和cm放到 for里面。
    可以存到map中map.put(cm,max);
    遍历map比较max的值。
    return最大的那个