Skip to main content
Back to problems
#3895
Medium Algorithms

Count digit appearances

86.6% acceptance
May 13, 2026
36
4
You are given an integer array nums and an integer digit. Return the total number of times digit appears in the decimal representation of all elements in nums.

Solution

Rust
Time O(n²)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn count_digit_occurrences(nums: Vec<i32>, digit: i32) -> i32 {
    let d = digit;
    let mut count = 0i32;
    for mut x in nums {
      if x == 0 {
        if d == 0 { count += 1; }
        continue;
      }
      while x > 0 {
        if x % 10 == d { count += 1; }
        x /= 10;
      }
    }
    count
  }
}