I want to return the characters that are being repeated as well as the number of times it occurs but my output isn't consistent with what I'm expecting as the output.
It's outputting e
6 times when it should be 4 times as well as outputting j
1 time when it should be 2 times. I'm aware I'm returning it the wrong way as well.
What am I doing wrong and how can I fix it?
public static String solution(String s) {
int i, j, count = 0;
for(i = 0; i < s.length(); i++) {
for(j = i + 1; j < s.length(); j++) {
if(s.charAt(i) == s.charAt(j)) {
System.out.print(s.charAt(i) + " ");
count++;
}
}
}
System.out.println();
System.out.println("no duplicates");
System.out.println("There are " + count + " repetitions");
return s;
}
public static void main(String args[]) {
String s = "eeejiofewnj";
solution(s);
}
output:
e e e e e e j
no duplicates
There are 7 repititions