beginnerDigit ProblemsJava

Find the Largest Digit in a Number

Find the maximum digit of an integer using arithmetic only.

Quick Answer

Peel off each digit with number % 10 and keep a running maximum, updating it whenever the current digit is larger. Drop the digit with number / 10 and repeat until the number is 0. Starting the maximum at 0 is safe because every digit is at least 0.

Problem Statement

Given an integer, return its largest digit. For example the digits of 5824 are 5, 8, 2, 4, and the largest is 8.

Scan the digits of the absolute value so negatives are handled the same as positives (-937 gives 9). Do not convert the number to a String.

Input: A single integer n.

Output: A single digit (0-9) — the largest digit of n.

Examples

Example 1
Input:  5824
Output: 8

Among 5, 8, 2, 4 the largest digit is 8.

Example 2
Input:  -937
Output: 9

The sign is ignored; among 9, 3, 7 the largest is 9.

Constraints

  • -2,147,483,648 <= n <= 2,147,483,647
  • No String conversion

Think Before You Code

Reveal the questions to ask yourself first
  • How do you look at each digit one at a time?
  • What starting value for a running maximum is always safe for digits?
  • When do you update that maximum?
  • Do negative signs change which digit is largest?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
Extract each digit with n % 10, then remove it with n / 10, looping while n is not 0.
Hint 2
Keep a max variable; each pass, if the current digit exceeds max, replace max.
Hint 3
Initialise max to 0 — no digit is below 0, so the first larger digit will overwrite it. Use the absolute value to handle negatives.

Approach

Reveal the step-by-step approach

Track the maximum digit while walking the number.

  1. Take the absolute value of n.
  2. Set max = 0 (every digit is at least 0).
  3. While the value is not 0:
    • digit = value % 10 — the current last digit.
    • If digit > max, set max = digit.
    • value = value / 10 — drop the digit.
  4. Return max.

Dry Run

Walk through the example step by step

Finding the largest digit of n = 5824:

step | value | digit = value % 10 | max
-----+-------+--------------------+----
start                             | 0
1    | 5824  | 4                  | 4
2    | 582   | 2                  | 4
3    | 58    | 8                  | 8
4    | 5     | 5                  | 8
end  | 0     | —                  | 8

Solution

Reveal the full Java solution
public class LargestDigit {
    public static int largestDigit(int n) {
        long value = Math.abs((long) n);
        int max = 0;
        while (value != 0) {
            int digit = (int) (value % 10);
            if (digit > max) {
                max = digit;
            }
            value /= 10;
        }
        return max;
    }

    public static void main(String[] args) {
        System.out.println(largestDigit(5824)); // 8
        System.out.println(largestDigit(-937)); // 9
    }
}

This is the running-maximum pattern applied to digits: seed max with a value no digit can fall below (0), then let each digit challenge it. Because a single digit is bounded to 0-9, the first digit alone would set a correct max, and the final answer is simply the largest challenger seen. Using the absolute value means the sign never affects which digit wins.

Time: O(d) where d is the number of digits (log10 n)Space: O(1)

Common Mistakes

  • Initialising max to a large value like 9, so it can never be updated downward — here that hides nothing, but the mirror mistake ruins the smallest-digit version.
  • Looping while (n > 0), which skips negatives.
  • Comparing the whole number instead of individual digits.

Edge Cases to Test

  • n = 0 returns 0 (its only digit).
  • Repeated digits like 7777 return 7.
  • A number whose largest digit is 9 anywhere returns 9.

Interview Follow-Ups

  • How would you also report the position of the largest digit?
  • How would you find the largest digit without a loop, comparing recursively?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us