Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 digitx is the number of times it appears in the decimal representation of n.
Example 1:
Input: n = 1553322
Output: 1
Explanation:
The least frequent digit in n is 1, which appears only once. All other digits appear twice.
Example 2:
Input: n = 723344511
Output: 2
Explanation:
The least frequent digits in n are 7, 2, and 5; each appears only once.
Constraints:
1 <= n <= 231 - 1Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Math
1553322
723344511
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3663: Find The Least Frequent Digit
class Solution {
public int getLeastFrequentDigit(int n) {
int[] cnt = new int[10];
for (; n > 0; n /= 10) {
++cnt[n % 10];
}
int ans = 0, f = 1 << 30;
for (int x = 0; x < 10; ++x) {
if (cnt[x] > 0 && cnt[x] < f) {
f = cnt[x];
ans = x;
}
}
return ans;
}
}
// Accepted solution for LeetCode #3663: Find The Least Frequent Digit
func getLeastFrequentDigit(n int) (ans int) {
cnt := [10]int{}
for ; n > 0; n /= 10 {
cnt[n%10]++
}
f := 1 << 30
for x, v := range cnt {
if v > 0 && v < f {
f = v
ans = x
}
}
return
}
# Accepted solution for LeetCode #3663: Find The Least Frequent Digit
class Solution:
def getLeastFrequentDigit(self, n: int) -> int:
cnt = [0] * 10
while n:
n, x = divmod(n, 10)
cnt[x] += 1
ans, f = 0, inf
for x, v in enumerate(cnt):
if 0 < v < f:
f = v
ans = x
return ans
// Accepted solution for LeetCode #3663: Find The Least Frequent Digit
fn get_least_frequent_digit(n: i32) -> i32 {
let mut n = n;
let mut freq = [i32::MAX; 10];
while n > 0 {
let m = (n % 10) as usize;
if freq[m] == i32::MAX {
freq[m] = 1;
} else {
freq[m] += 1;
}
n /= 10;
}
let mut ret = 0;
let mut min_freq = i32::MAX;
for (i, n) in freq.into_iter().enumerate() {
if n < min_freq {
ret = i as i32;
min_freq = n;
}
}
ret
}
fn main() {
let ret = get_least_frequent_digit(723344511);
println!("ret={ret}");
}
#[test]
fn test() {
assert_eq!(get_least_frequent_digit(1553322), 1);
assert_eq!(get_least_frequent_digit(723344511), 2);
}
// Accepted solution for LeetCode #3663: Find The Least Frequent Digit
function getLeastFrequentDigit(n: number): number {
const cnt: number[] = Array(10).fill(0);
for (; n; n = (n / 10) | 0) {
cnt[n % 10]++;
}
let [ans, f] = [0, Number.MAX_SAFE_INTEGER];
for (let x = 0; x < 10; ++x) {
if (cnt[x] > 0 && cnt[x] < f) {
f = cnt[x];
ans = x;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.