// Tyler Rigsby, CSE 142 // Demonstrates tallying; allows you to compute the most frequent digit in a number. public class Tallying1 { public static void main(String[] args) { int n = 669260269 int digit = mostFrequentDigit(n); System.out.println("Most frequent digit in " + n + " is: " + digit); } // Returns the most frequent digit in a number. In the // case of a tie, returns the first most frequent digit. public static int mostFrequentDigit(int n) { int[] digits = new int[10]; // while there are still digits while (n > 0) { int digit = n % 10; digits[digit]++; n /= 10; } int bestDigit = 0; for (int i = 0; i < digits.length; i++) { if (digits[i] > digits[bestDigit]) { bestDigit = i; } } return bestDigit; } }