[LeetCode] Find The Least Frequent Digit

3663. Find The Least Frequent Digit

Given an integer n, find the digit that occurs least frequently in its decimal representation. If multiple digits have the same frequency, choose the smallest digit.

Return the chosen digit as an integer.

The frequency of a digit x is the number of times it appears in the decimal representation of n.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int getLeastFrequentDigit(int n) {
unordered_map<int,int> freq;
while(n) {
freq[n%10]++;
n /= 10;
}
int res = begin(freq)->first;
for(auto& [k,v] : freq) {
if(v == freq[res]) res = min(res,k);
else if(v < freq[res]) res = k;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/09/03/PS/LeetCode/find-the-least-frequent-digit/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.