import java.util.*; public class Ex05Mode2 { public static int mode(int[] list) { Arrays.sort(list); int mode = -1; // doesn't matter int frequency = 0; // nonsense value for (int i = 0; i < list.length; i++) { int num = list[i]; int count = 1; for (i++; i < list.length && num == list[i]; i++) { count++; } i--; if (count > frequency) { mode = num; frequency = count; } } return mode; } public static void main(String[] args) { // numbers in list are guaranteed: 0 <= n <= 100 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) ); } }