Question: Could somebody write comments for each line of code explaining what each line does? I got this code from the book and it tells you
Could somebody write comments for each line of code explaining what each line does? I got this code from the book and it tells you which numbers come up the most frequently in arrays. I just need help comprehending each of the loops and if/else variables
import java.util.*;
public class lastIndexOf
{
public static void main(String[] args){
int a[]={27, 15, 15, 11, 27};
int b[]={27, 15, 15, 11, 27,15};
int c[]={27, 15,11, 15, 11, 27};
System.out.println("The mode of a is " + mode(a));
System.out.println("The mode of b is "+ mode(b));
System.out.println("The mode of c is " + mode(c));
}
public static int mode(int[] array) {
int[] spareArray = new int[101];
for (int i = 0; i < array.length; i++) {
spareArray[array[i]]++;
}
int mode = 101;
int count = 0;
for (int i = 0; i < spareArray.length; i++) {
if (spareArray[i] > count) {
count = spareArray[i];
mode = i;
}
}
return mode;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
