// Tyler Rigsby, CSE 142 // Demonstrates the use of arrays for tallying public class Tallying { public static void main(String[] args) { int val = 778294282; int mfd = mostFrequentDigit(val); System.out.println("Most frequent digit of " + val + " is " + mfd); } // Takes an integer as a parameter and returns the most frequent digit // in the integer public static int mostFrequentDigit(int n) { // make an array such that: // element 0 -> count of digit 0 // element 1 -> count of digit 1 // etc... int[] countDigits = new int[10]; // fill the array while (n > 0) { int digit = n % 10; countDigits[digit]++; n /= 10; } // search the array for the most frequent digit (modified cumulative max) int max = 0; for (int i = 0; i < countDigits.length; i++) { if (countDigits[i] > countDigits[max]) { max = i; } } return max; } }