Back to Practice Dashboard
Python BasicsEasy

Finding Number of times x digit occurs

Learn how to solve the 'Finding Number of times x digit occurs' problem. This detailed resource details brute force and optimized approaches.

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?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

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

Open in Editor