[LeetCode] Digit Frequency Score

3945. Digit Frequency Score

You are given an integer n.

The score of n is defined as the sum of d * freq(d) over all distinct digits d, where freq(d) denotes the number of times the digit d appears in n.

Return an integer denoting the score of n.

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