public class Ex05Mode { public static int mode(int[] list) { int[] tally = new int[101]; // The VALUE in the list serves as the INDEX into the tally array. for (int n : list) { tally[n]++; } int mode = tally[0]; int frequency = 1; for (int i = 0; i < tally.length; i++) { int count = tally[i]; if (count > frequency) { // use >= to break ties with the higher value frequency = count; mode = i; } } return mode; } public static void main(String[] args) { int[] list1 = { 27, 15, 15, 11, 27, 15, 27, 27}; // 27 is the mode int[] list2 = { 27, 15, 15, 11, 27, 15, 27}; // 15 and 27 are tied System.out.println( mode(list1) ); System.out.println( mode(list2) ); } }