Foundations Working with digits and divisors
29. Largest Digit
Easy
Given an integer n, return its largest digit.
A negative number has the same digits as its absolute value, because the minus sign is not a digit. So the answer is always a digit from 0 to 9, never negative. The number 0 has a single digit, 0, so the answer for 0 is 0.
Example 1
Input: n = 52817
Output:
8Explanation: The digits are 5, 2, 8, 1 and 7, and the largest of them is 8.
Example 2
Input: n = -1093
Output:
9Explanation: The minus sign is not a digit. The digits are 1, 0, 9 and 3, so the answer is 9.
Example 3
Input: n = 0
Output:
0Explanation: 0 has one digit, 0.
Check your understanding
Work out the answer for this input before you write any code.
n = 3092
Constraints
-9 × 1015 ≤ n ≤ 9 × 1015
Clarifying questions
What you would ask an interviewer before writing any code.