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

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

    In java... using for loop:

    import java.util.Scanner;
    
    /**
     *
     * @author MD SADDAM HUSSAIN */
    public class Learn {
    
        public static void main(String args[]) {
    
            Scanner sc = new Scanner(System.in);
            String input = sc.next();
            char process[] = input.toCharArray();
            boolean status = false;
            int index = 0;
            for (int i = 0; i < process.length; i++) {
                for (int j = 0; j < process.length; j++) {
    
                    if (i == j) {
                        continue;
                    } else {
                        if (process[i] == process[j]) {
                            status = true;
                            index = i;
                            break;
                        } else {
                            status = false;
    
                        }
                    }
    
                }
                if (status) {
                    System.out.print("" + process[index]);
    
                }
            }
        }
    }
    

提交回复
热议问题