Finding Number of times x digit occurs
Detailed guide and Python implementation for the 'Finding Number of times x digit occurs' problem.
Problem Statement
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.
- •0 <= n <= 10^9
- •0 <= x <= 9
Examples
count_digit(122333, 3)
3
The digits of 122333 are 1,2,2,3,3,3. The digit 3 appears 3 times.
count_digit(1000, 0)
3
The digits of 1000 are 1,0,0,0. The digit 0 appears 3 times.
count_digit(12345, 6)
0
The digit 6 does not appear in 12345.
Need a Hint?
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.
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Regex
Master Regular Expressions (Regex) in Python. Learn to search, match, split, and substitute string data using the built-in re library.
How to Find the Length of a List in Python
Learn how to find the length of a list in Python using the len() function. Understand the O(1) time complexity and checking counts.
Python Regex Patterns (re module)
Reference guide for Python regular expressions. Learn match, search, findall, sub, and essential regex patterns.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.