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

前端 未结 30 2527
遇见更好的自我
遇见更好的自我 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:44

      public class StringCountwithOutHashMap {
      public static void main(String[] args) {
        System.out.println("Plz Enter Your String: ");
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        int count = 0;
        for (int i = 0; i < s1.length(); i++) {
            for (int j = 0; j < s1.length(); j++) {
                if (s1.charAt(i) == s1.charAt(j)) {
                    count++;
                } 
            } 
            System.out.println(s1.charAt(i) + " --> " + count);
            String d = String.valueOf(s1.charAt(i)).trim();
            s1 = s1.replaceAll(d, "");
            count = 0;
        }}}
    

提交回复
热议问题