Я получил фиксированный массив в своей задаче
но когда я меняю положение чисел в моем массиве
Мой алгоритм не работает
вот мой код:
Код: Выделить всё
class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 2, 2, 3, 3, 3, 3};
int maxCnt = Integer.MIN_VALUE;
int mem = arr[0];
int cur = arr[0];
int curCnt = 1;//you can modify this line with any value but it has to be curCnt =
int mode = arr[0];
int i;
for(i=1;i maxCnt) {
mode = mem;
maxCnt = curCnt;
}
/*mem = cur; you can modify in these two lines
curCnt = 1;*/
}
}
if(curCnt > maxCnt/*you can modify in this block*/) {
mode = mem;
maxCnt = curCnt;
}
System.out.printf("mode = %d%nfreq = %d", mode, maxCnt);
}
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... de-in-java