Find duplicate characters in a String and count the number of occurances using Java

前端 未结 30 2542
遇见更好的自我
遇见更好的自我 2020-12-14 11:47

How can I find the number of occurrences of a character in a string?

For example: The quick brown fox jumped over the lazy dog.

Some example

30条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-14 12:29

     void Findrepeter(){
        String s="mmababctamantlslmag";
        int distinct = 0 ;
    
        for (int i = 0; i < s.length(); i++) {
    
            for (int j = 0; j < s.length(); j++) {
    
                if(s.charAt(i)==s.charAt(j))
                {
                    distinct++;
    
                }
            }   
            System.out.println(s.charAt(i)+"--"+distinct);
            String d=String.valueOf(s.charAt(i)).trim();
            s=s.replaceAll(d,"");
            distinct = 0;
    
        }
    
    }
    

提交回复
热议问题