Im trying to find the mode of an int array. I have come up with an algorithm that i feel should work, but it isn't. I get the following error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Statistik.typvärde(Statistik.java:157)
at Statistik.main(Statistik.java:17)
Here is my code:
public static int typvärde(int list[]) {
int oc[] = new int[list.length];
int counter = 0;
int typvärde = 0;
int max = 0;
int typindex = 0;
for(int i = 0; i < list.length; i++) {
for(int j = 0; j < list.length; j++) {
if(list[i] == list[j]) {
counter++;
}
oc[i] = counter;
}
}
max = Maxmin.max(oc);//class function that finds max of an array
typindex = Arrays.asList(oc).indexOf(max);
typvärde = list[typindex];
return typvärde;
}