I want my code to display each letters frequency but instead, I'm getting an ArrayIndexOutOfBoundsException
. I'm having trouble spotting what I did wrong.
How can I rectify this?
Here's my code:
public static void solution(String s) {
char[] c = s.toCharArray();
int j = 0, i = 0, counter = 0;
for(i = 0; i < c.length; i++) {
counter = 0;
for(j = 0; j < c.length; j++) {
if(c[j] == c[i]) {
counter++;
}
}
}
System.out.println("The letter " + c[j] + " appears " + counter + " times");
}
public static void main(String args[]) {
String s = "abaababcdelkm";
solution(s);
}