Problem overview
The method biggestDigitSum receives an array of integers nums and returns the number whose digits add up to the most. The digit sum of 472 is 4 + 7 + 2 = 13. If two numbers have the same digit sum, the one that comes first in the array wins. The method returns the number itself, not its digit sum. Every number is between 0 and 1,000,000,000, and the array has at least one number.
For [472, 95, 1000] the digit sums are 13, 14 and 1, so the answer is 95. For [19, 55, 28] all three sums are 10, so the first number, 19, wins the tie. For [0, 7] the sums are 0 and 7, so the answer is 7.
Approach: a helper for the sum, a loop for the winner
The lesson splits the work into two methods. The helper digitSum takes one number and returns the sum of its digits. The graded method biggestDigitSum uses it to compare the numbers.
Finding the digits of a number relies on two operations. n % 10 is the last digit: 472 % 10 is 2. Dividing by 10 and rounding down drops the last digit: Math.floor(472 / 10) is 47. Repeating both until the number reaches 0 visits every digit from the right.
The rounding matters because TypeScript's number is a floating-point type. 472 / 10 is 47.2, not 47, so without Math.floor the number never becomes a whole number again.
The main method then keeps the best number seen so far. It starts with the first number and replaces it only when a number has a strictly bigger digit sum, which is what makes the earlier number win a tie.
Step by step
- In
digitSum, declaretotalas 0. Ifnis 0 the loop never runs, and 0 is the right answer. - Loop while
n > 0. Each turn removes one digit, so the loop ends once all digits are gone. - Add
n % 10tototal. That is the last digit. - Set
ntoMath.floor(n / 10). Without this linennever shrinks properly. - Return
total. - In
biggestDigitSum, declarebestasnums[0]. Before any comparison, the first number is the best one seen. - Loop over
numswithfor (const num of nums). The first turn compares the first number with itself, which changes nothing. - Compare the digit sums with
this.digitSum(num) > this.digitSum(best), and setbest = numwhen it is true. The sign is>, not>=, so a tie keeps the earlier number. - Return
best, the number and not its sum.
The solution in TypeScript
class Solution {
digitSum(n: number): number {
let total: number = 0;
while (n > 0) {
total += n % 10;
n = Math.floor(n / 10);
}
return total;
}
biggestDigitSum(nums: number[]): number {
let best: number = nums[0];
for (const num of nums) {
if (this.digitSum(num) > this.digitSum(best)) {
best = num;
}
}
return best;
}
}
What to notice in TypeScript:
- Parameters are variables.
n = Math.floor(n / 10)changes the helper's own copy ofn. The caller's number is not affected, because numbers are passed by value. this.before the helper. Inside the class,digitSumis reached asthis.digitSum.- No overflow worries. A
numberis a double, and whole numbers are exact up to 9,007,199,254,740,991. The largest input here, 1,000,000,000, is far below that.
Tracing the helper on 472
n before | last digit | total after | n after |
|---|---|---|---|
| 472 | 2 | 2 | 47 |
| 47 | 7 | 9 | 4 |
| 4 | 4 | 13 | 0 |
Now n is 0, the loop condition n > 0 fails, and the helper returns 13.
Another way to get the digits
A number can also be turned into text and read one character at a time. Each character is converted back to a number and added:
function digitSum(n: number): number {
let total: number = 0;
for (const ch of String(n)) {
total += Number(ch);
}
return total;
}
It gives 13, 14, 1 and 0 for 472, 95, 1000 and 0. The lesson uses % 10 and Math.floor because that approach works on the number itself and is the one you will meet again in other problems.
Digit sums of the examples
Calling the helper gives digitSum(472) = 13, digitSum(95) = 14, digitSum(1000) = 1, digitSum(0) = 0 and digitSum(1000000000) = 1. Zeros in the middle or at the end add nothing, and 0 itself gives 0 because the loop never runs.
Edge cases and common mistakes
- Forgetting
Math.floor. After the first stepnis 47.2, then 4.72, and so on through smaller and smaller fractions. The digits you add are wrong, and the loop keeps running through more than a hundred smaller and smaller fractions after the last real digit. - Using
>=in the comparison. On[19, 55, 28]that version returns 28, because each later tie replaces the best. The task wants 19. - Returning the sum.
digitSum(95)is 14, but the answer for[472, 95, 1000]is 95. - Zero in the array. The helper returns 0 for 0, so a zero never beats a positive number and only wins when it is the only number or every sum is 0.
- Starting
bestat a made-up value. The answer must be an element ofnums. Starting fromnums[0]guarantees that, whatever the array holds.
Testing
[472, 95, 1000]gives95.[19, 55, 28]gives19.[0, 7]gives7.
