Answers for "how to print the duplicate characters from the given string java"

3

counting repeated characters in a string in java

String s="mmababctamantlslwgikl";
    int distinct = 0 ;

    while(s.length()>0){
        
        for (int j = 0; j < s.length(); j++) {

            if(s.charAt(0)==s.charAt(j))
            {
                distinct++;

            }
        }   
        
        System.out.println(s.charAt(0)+"--"+distinct);
        String d=String.valueOf(s.charAt(0)).trim();
        s=s.replaceAll(d,"");
        distinct = 0;

        
    }
Posted by: Guest on March-31-2022
-1

java, how to find the most repeated character

private static String findMaxChar(String str) {
    char[] array = str.toCharArray();
    Set<Character> maxChars = new LinkedHashSet<Character>();

    int maxCount = 1;
    maxChars.add(array[0]);

    for(int i = 0, j = 0; i < str.length() - 1; i = j){
        int count = 1;
        while (++j < str.length() && array[i] == array[j]) {
            count++;
        }
        if (count > maxCount) {
            maxCount = count;
            maxChars.clear();
            maxChars.add(array[i]);
        } else if (count == maxCount) {
            maxChars.add(array[i]);
        }
    }

    return (maxChars + " = " + maxCount);
}
Posted by: Guest on January-02-2021

Code answers related to "how to print the duplicate characters from the given string java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language