I got an assignment that my professor has given me the code but I have to fill the answers in the assigned line to complete the code
I got with the fixed array in my problem but when I rearrange position of the numbers in my array My algorithm doesn’t work
here’s my code:
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<arr.length;i++) {
cur = arr[i];
if(cur == mem) {
curCnt++;
}
else {
if(curCnt > 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);
}
}
The answers have to be filled in where I commented in the code