java 2c how to find the most repeated character

Solutions on MaxInterview for java 2c how to find the most repeated character by the best coders in the world

showing results for - "java 2c how to find the most repeated character"
Lara
22 Jan 2018
1private static String findMaxChar(String str) {
2    char[] array = str.toCharArray();
3    Set<Character> maxChars = new LinkedHashSet<Character>();
4
5    int maxCount = 1;
6    maxChars.add(array[0]);
7
8    for(int i = 0, j = 0; i < str.length() - 1; i = j){
9        int count = 1;
10        while (++j < str.length() && array[i] == array[j]) {
11            count++;
12        }
13        if (count > maxCount) {
14            maxCount = count;
15            maxChars.clear();
16            maxChars.add(array[i]);
17        } else if (count == maxCount) {
18            maxChars.add(array[i]);
19        }
20    }
21
22    return (maxChars + " = " + maxCount);
23}
24