Mode | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

11th Jul 2019, 9:27 PM
Beanie
Beanie - avatar
4 Answers
+ 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.
11th Jul 2019, 9:46 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 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; } }
11th Jul 2019, 9:40 PM
Anton Böhler
Anton Böhler - avatar
+ 2
but methods and variables should always start with a lowercase letter....
11th Jul 2019, 9:41 PM
Anton Böhler
Anton Böhler - avatar
+ 1
if you want compare all numbers, move return after loops: for( ) { for( ) { } } return maxNum; //here
12th Jul 2019, 8:23 AM
zemiak