Python BasicsEasy

Finding Number of times x digit occurs

Detailed guide and Python implementation for the 'Finding Number of times x digit occurs' problem.

Problem Statement

Easy

Write a function count_digit(n, x) that takes a non-negative integer n and a digit x (0-9), and returns the number of times digit x appears in n.

For example, in the number 122333, the digit 3 appears 3 times.

Constraints
  • 0 <= n <= 10^9
  • 0 <= x <= 9

Examples

Example 1
Input
count_digit(122333, 3)
Output
3
Explanation

The digits of 122333 are 1,2,2,3,3,3. The digit 3 appears 3 times.

Example 2
Input
count_digit(1000, 0)
Output
3
Explanation

The digits of 1000 are 1,0,0,0. The digit 0 appears 3 times.

Example 3
Input
count_digit(12345, 6)
Output
0
Explanation

The digit 6 does not appear in 12345.

Need a Hint?
Consider using Numbers-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.