char [] array = {a,a,a,b,b,c,c,c,a,d};
I want to count every same element in that array so I can sort it from highest frequency to the lowest one. I want the output become like this:
4 (for a)
2 (for b)
3 (for c)
1 (for d)
I have try this
public static void CountbyChar(String s){
int [] arr = new int [s.length()];
char [] c =s.toCharArray();
for (int i=0;i<c.length;i++){
arr[i]=1;
for (int j=i+1;j<c.length;j++){
if(c[i]==c[j]){
arr[i]++;
}
}
}
for (int x:arr){
System.out.println(x);
}
}
But I got:
4
3
2
2
1
2
1
1
Where is my fault?