0
Mode
I’m trying to create a method to find the mode of an array. I created the method but I dont know how to test it out . https://code.sololearn.com/c1v9Yc24w5u6/?ref=app
4 Respuestas
+ 4
You were implementing a function inside another(main) function which is not allowed.
That's why compiler was throwing an error.
Anton Böhler has corrected your code, now test to see if it meets your desired output.
+ 2
some minor bugs... now it should work, not sure if it now does what you want ... 😅😅
happy coding!😄
public class Frequently {
public static void main(String []args)
{
int [] arr = {3,3,4,6,7,9,10};
int maxNum = Mode(arr);
System.out.println("The mode is " + maxNum);
}
public static int Mode(int[] arr){
int maxNum = -1;
int Apperances = -1;
for (int counter = 0; counter < arr.length;counter++) {
int count = 0;
for ( int i = 0; i < arr.length ;i++) {
if (arr[counter] == arr[i] )
count ++;
if(count > Apperances) {
maxNum = arr[counter];
Apperances = count;
}
return maxNum;
}
}
return -1;
}
}
+ 2
but methods and variables should always start with a lowercase letter....
+ 1
if you want compare all numbers, move return after loops:
for( ) {
for( ) {
}
}
return maxNum; //here