Find the mode (most frequent value in an array) using a simple for loop?
问题 How do I find the mode (most frequent value in an array) using a simple for loop? The code compiles with a wrong output. Here is what I have: public static void mode(double [] arr) { double mode=arr[0]; for(int i = 1; i<arr.length; i++) { if(mode==arr[i]) { mode++; } } return mode; } 回答1: First I sort the array by order and then I count occurrences of one number. No hashmaps only for loop and if statements. My code: static int Mode(int[] n){ int t = 0; for(int i=0; i<n.length; i++){ for(int j