it keeps repeating regardless of whether it's been already calculated or not. You can see the example output. it already calculated the occurrence of 1, but when it sees 1 again, it will calculate it again!
public class SortingInLinearTime {
public static int[][] howMany(int[] n){
// Makes a double array list where the second value is occurrence of the first value.
int[][] a = new int[n.length][2];
int counter = 0;
for(int i = 0; i != n.length; i++){
for(int j = 0; j != n.length; j++) {
if(n[i] == n[j]){
counter++;
}
}
a[i][0] = n[i];
a[i][1] = counter;
counter = 0;
}
// printer helper function
for(int i = 0; i != n.length; i++){
System.out.print(a[i][0] + " occurs ");
System.out.println(a[i][1] + " times");
}
return a;
}
public static void main(String[] args) {
int[] testArray = {1, 2, 3, 1, 2, 3, 4};
System.out.print(howMany(testArray));
}
}
output: 1 occurs 2 times 2 occurs 2 times 3 occurs 2 times 1 occurs 2 times 2 occurs 2 times 3 occurs 2 times 4 occurs 1 times [[I@15db9742