Reduce a Number to One Digit
Problem statement
Given a non-negative integer num, repeatedly replace it with the sum of its decimal digits until only one digit remains.
Return that final single digit. If num is already a single digit, return it unchanged.
Function
reduceToOneDigit(num: int) → intExamples
Example 1
num = 38return = 2The first digit sum is 3 + 8 = 11. Summing again gives 1 + 1 = 2.
Example 2
num = 0return = 00 is already a single digit, so it is returned unchanged.
Example 3
num = 9998return = 8The reductions are 9 + 9 + 9 + 8 = 35 and then 3 + 5 = 8.
Constraints
0 <= num <= 10^9.