Answer:
The method definition to this question can be given as:
Method definition:
public static int mode(int[] a1) //define method mode.
{
int[] a = new int[101]; //define array.
int i,mode =101,counts = 0; ; //define variable.
for (i = 0; i <a1.length; i++) //loop
{
a[a1[i]]++; //increasing value.
}
for (i = 0; i <a.length; i++)//loop
{
if (a[i]>counts)//if block
{
counts=a[i]; //variable holds array value.
mode = i; //variable holds i value.
}
}
return mode;
}
Explanation:
The description of the above method as follows:
- Firstly we define a method that is "mode" in this method we pass an integer array that is "a1[]" and the method returns an integer value because its return type is "integer".
- Inside a method, we define another array and variables that are "a[], mode, i, and counts". array variables collect the parameter passed the array and check the lowest value element in the array. To check this condition we use these variables and conditional statements.
- Then we define a loop inside the loop use conditional statement in the if block we check array element value is grater than counts if this condition is true so, counts variable value is changed by array value and mode variable holds loop variable (i)value and return mode value.