#3663
Easy Algorithms Find the least frequent digit
Array Hash Table Math Counting
69.7% acceptance
Feb 25, 2026
45
2
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn get_least_frequent_digit(n: i32) -> i32 {
let mut freq = [0u32; 10];
let mut x = n;
while x > 0 {
freq[(x % 10) as usize] += 1;
x /= 10;
}
let min_freq = *freq.iter().filter(|&&f| f > 0).min().unwrap();
(0..10).find(|&d| freq[d] == min_freq).unwrap() as i32
}
}