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: 8

Explanation: The digits are 5, 2, 8, 1 and 7, and the largest of them is 8.

Example 2

Input: n = -1093

Output: 9

Explanation: 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: 0

Explanation: 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.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.