Start learning free

LESSON 216 OF 285 · WHILE LOOPS - CHALLENGES

Number of Digits in Go: Counting with a for Loop

Practice Read the full explanation

Given a non-negative integer n, return how many digits it has.

The digits are the figures from 0 to 9 that n is written with. Dividing a number by 10 without the remainder removes its last digit (542 becomes 54), so you can count how many times that happens until the number is 0. The number 0 itself has one digit.

 

Example 1:

Input: n = 542
Output: 3
Explanation: 542 is written with three digits: 5, 4 and 2.

Example 2:

Input: n = 100
Output: 3
Explanation: The zeros are digits too: 1, 0 and 0.

Example 3:

Input: n = 0
Output: 1
Explanation: 0 is written with one digit.

 

Constraints:

  • 0 <= n <= 109
STEP 1 OF 6
    Ctrl + Enter to check

    GO · WHILE LOOPS - CHALLENGES

    No Of Digits in Go, explained

    Problem overview

    The function noOfDigits receives a non-negative integer n and returns how many digits it is written with. 542 has 3 digits, and 100 has 3 too, because zeros are digits. The number 0 is a special case: it is written with one digit, so the answer is 1.

    Approach

    Dividing an int by 10 with / drops the remainder, which removes the last digit: 542 becomes 54. Count how many times you can do that before the number reaches 0, and you have the number of digits. Go has no while, so the repeat is a for loop with only a condition: for n > 0.

    That count is right for every positive number but wrong for 0, where the loop wouldn't run at all and the count would stay 0. So 0 gets its own early return.

    For n = 542, the values after each pass are:

    Passn afterwardscount
    1541
    252
    303

    Step by step

    1. Handle zero first. If n == 0, return 1. The loop below only runs while the number is greater than 0, so it would count no digits for 0.
    2. Create count with :=, starting at 0. No digit has been removed yet.
    3. Write the loop with the condition n > 0, and its closing brace right away. Each pass removes one digit.
    4. Add 1 to count with ++. One pass is one digit.
    5. Drop the last digit with n = n / 10. Once every digit is gone, n is 0 and the loop stops.
    6. Return count after the loop.

    The solution in Go

    func noOfDigits(n int) int {
        if n == 0 {
            return 1
        }
        count := 0
        for n > 0 {
            count++
            n = n / 10
        }
        return count
    }
    

    Go details:

    • count++ is a statement. In Go, ++ can't be used inside an expression, so x := count++ doesn't compile. It stands on its own line.
    • Integer division. Both operands are int, so / truncates. 542 / 10 is 54, not 54.2.
    • The parameter is a copy. Changing n inside the function doesn't affect the caller.

    Complexity

    The loop runs once per digit, so the time grows with the number of digits, and the space is one counter. For inputs up to a billion, that is at most ten passes.

    Another way

    You can also turn the number into text and measure it. In a file of your own, with strconv imported, len(strconv.Itoa(n)) gives 3 for 542, 3 for 100, 1 for 0 and 10 for 1000000000, so it needs no special case for 0. The lesson uses the division loop because it practises for and integer arithmetic, and it never builds a string.

    Edge cases and common mistakes

    • Zero. Without the early return, noOfDigits(0) gives 0 instead of 1.
    • Zeros inside and at the end. noOfDigits(100) returns 3, because each division removes one digit regardless of its value.
    • Largest input. noOfDigits(1000000000) returns 10.
    • Forgetting to update n. If the division line is missing, the loop never ends for a positive n.

    Testing

    • 542 gives 3.
    • 100 gives 3.
    • 0 gives 1.
    • 7 gives 1.
    • 1000000000 gives 10.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free