[LeetCode] Count Digit Appearances

3895. Count Digit Appearances

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int countDigitOccurrences(vector<int>& nums, int digit) {
int res = 0;
for(auto& n : nums) {
while(n) {
res += digit == (n % 10);
n /= 10;
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/count-digit-appearances/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.