I made a program that counts the elements in an array. It works but there is a sort of error in my program.
I want the output of my program is like this:
1 occured: 2times
2 occured: 1times
3 occured: 1times
6 occured: 1times
but my program gives an output of this:
1 occured: 1times
1 occured: 2times
2 occured: 1times
3 occured: 1times
6 occured: 1times
String[] values= {"1", "1", "3", "6", "2"};
int[] counts = new int[values.length];
Arrays.sort(values);
int temp = 0;
int c = 0;
for(int i = 0; i < values.length; i++){
counts[i] = Integer.parseInt(values[i]);
for(int j = 0;j < counts.length; j++) {
if(counts[i] == counts[j]) {
c++;
}
}
System.out.println(counts[i] + " occured: " + c +" times");
c = 0;
}