Digit Tally
Return how many times the digit d appears when the non-negative integer n is written in decimal. The number 0 is written as the single digit 0, so n = 0 with d = 0 gives 1.
You may convert n to a string or loop over its digits, but try the recursive view: the tally for n is the tally of its last digit plus the tally for n // 10, and a number below 10 is its own last digit.
For example, n = 7077 and d = 7 give 3.
Example 1
n 7077
d 7
returns 3
7077 has the digits 7, 0, 7 and 7, so the digit 7 appears three times.
Example 2
n 123
d 4
returns 0
None of the digits 1, 2 and 3 is a 4.
Constraints
0 <= n <= 10000000000 <= d <= 9
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Recursion problems
- Equal NeighboursBeginner
- Halving TotalBeginner
- Largest DigitBeginner
- Multiply It OutBeginner
- Nested SumBeginner
- Rising DigitsBeginner
- Ruler PatternBeginner
- Same Both WaysBeginner