Start learning free

LESSON 248 OF 675 · HELPER FUNCTIONS

Biggest Digit Sum in TypeScript: Two Methods Working Together

Practice Read the full explanation

You get an array of integers nums. Return 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, return the one that comes first.

The work is split between two functions. The first one is a helper. It gets one number and returns the sum of its digits. The second one is the function that is graded. It uses the helper to compare the numbers.

 

Example 1:

Input: nums = [472, 95, 1000]
Output: 95
Explanation: The digit sums are 13, 14 and 1.
95 has the biggest one: 9 + 5 = 14.

Example 2:

Input: nums = [19, 55, 28]
Output: 19
Explanation: All three digit sums are 10.
19 comes first, so it wins the tie.

Example 3:

Input: nums = [0, 7]
Output: 7
Explanation: The digit sum of 0 is 0.
The digit sum of 7 is 7.

 

Constraints:

  • nums has at least one number.
  • Every number is between 0 and 109.
  • Return the number itself, not its digit sum.
STEP 1 OF 9
    Ctrl + Enter to check

    TYPESCRIPT · HELPER FUNCTIONS

    Biggest Digit Sum in TypeScript, explained

    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

    1. In digitSum, declare total as 0. If n is 0 the loop never runs, and 0 is the right answer.
    2. Loop while n > 0. Each turn removes one digit, so the loop ends once all digits are gone.
    3. Add n % 10 to total. That is the last digit.
    4. Set n to Math.floor(n / 10). Without this line n never shrinks properly.
    5. Return total.
    6. In biggestDigitSum, declare best as nums[0]. Before any comparison, the first number is the best one seen.
    7. Loop over nums with for (const num of nums). The first turn compares the first number with itself, which changes nothing.
    8. Compare the digit sums with this.digitSum(num) > this.digitSum(best), and set best = num when it is true. The sign is >, not >=, so a tie keeps the earlier number.
    9. 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 of n. The caller's number is not affected, because numbers are passed by value.
    • this. before the helper. Inside the class, digitSum is reached as this.digitSum.
    • No overflow worries. A number is 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 beforelast digittotal aftern after
    4722247
    47794
    44130

    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 step n is 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 best at a made-up value. The answer must be an element of nums. Starting from nums[0] guarantees that, whatever the array holds.

    Testing

    • [472, 95, 1000] gives 95.
    • [19, 55, 28] gives 19.
    • [0, 7] gives 7.
    Keep going with TypeScript500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free