// example of searching an array public class array_mode { // Return mode of non-empty array $a$ of non-negative numbers. // For efficiency, assume the elements of $a$ are *dense*, // i.e. do not have huge gaps, otherwise $tally$ is *huge*: public static int mode(int[] a) { // Set max to largest element of $a$: int max = a[0]; // max element of $a$ for (int i = 1; itally[j]) j = i; // Return mode: return j; } // method mode // Test example for finding mode: public static void main(String[] args) { int[] x = {1,2,2,3,3,3,5}; // note: 3 is the mode of $x$ System.out.println("Mode: " + mode(x) ); // report mode of $x$ } // method main } // class array_mode