Count number of odd digits in a number

Pattern: Maths basics

easy

Count how many of a number's digits are odd, using the last digit and a remainder check.

The problem

You are given a whole number n that is zero or more and fits in a 32-bit integer. Return how many of its decimal digits are odd (1, 3, 5, 7 or 9). For example, 12345 has three odd digits, and 2468 has none.

On the original site the problem is written a little differently, so use the link above for its exact wording. The function in this guide is called countOddDigits.

Think first

On paper, list the digits of 12345 and mark the odd ones.

  • Which operation hands you the last digit of 12345 as a number you can test?
  • How do you test whether a single digit is odd?
  • After you have looked at the last digit, what do you do to move on to the next one?

Hints

    Brute force, and why it fails

    You could turn the number into text and test each character, or store every digit in a list first and then count the odd ones. Both work and both are fast enough, because a number has at most 10 digits.

    The arithmetic version needs no extra storage and uses the same two moves, % 10 and / 10, that you will meet again in reversing a number, palindromes and digit sums. That reuse is the reason to practise it here.

    The key insight

    Every round, n % 10 shows you exactly one digit and n / 10 removes it. Looking at each digit once, from the right, is enough to count the odd ones.

    Dry run

    10 
    21 
    32 
    43 
    54 
    n12345
    count0

    Start with n = 12345 and a count of 0. The digits are shown left to right.

    Step 1 of 7

    The plan

    count = 0
    while n > 0:
        digit = n mod 10        # the last digit
        if digit mod 2 == 1:    # odd
            count = count + 1
        n = n div 10            # drop the last digit
    return count
    

    Complexity

    Time
    O(d)
    Space
    O(1)

    d is the number of digits and every digit is looked at once. The loop keeps only a few variables, so the extra space is constant.

    Edge cases

    • n = 0 has no odd digits. The loop does not run and the answer is 0, so no special case is needed here.
    • A number with only even digits, such as 2468, returns 0.
    • A number with only odd digits, such as 13579, returns its digit count (5).
    • Trailing zeros such as in 100 are even digits and are skipped; the answer is 1.
    I've built my logic, show the code

    Java

    class Solution {
        public int countOddDigits(int n) {
            int count = 0;
            while (n > 0) {
                int digit = n % 10;
                if (digit % 2 == 1) {
                    count++;
                }
                n = n / 10;
            }
            return count;
        }
    }

    C++

    class Solution {
    public:
        int countOddDigits(int n) {
            int count = 0;
            while (n > 0) {
                int digit = n % 10;
                if (digit % 2 == 1) {
                    count++;
                }
                n = n / 10;
            }
            return count;
        }
    };

    Python

    class Solution:
        def countOddDigits(self, n):
            count = 0
            while n > 0:
                digit = n % 10
                if digit % 2 == 1:
                    count += 1
                n = n // 10
            return count

    Common mistakes

    • Testing n % 2 instead of digit % 2. That checks only whether the whole number is odd.
    • Forgetting to update n inside the loop, which never stops.
    • Counting the digit before checking it is odd.
    • Dividing with / in Python 3 instead of //.

    Similar problems