Skip to main content

Find The Least Frequent Digit - Solution & Explanation

EasyArrayHash TableMathCounting7 min readAsked at: Flipkart
Practice this problem

Problem Statement

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.

 

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​​​​​​​ - 1

Approach Overview

Problem Overview: You are given a number and need to determine which digit appears the fewest times. The task is essentially a frequency analysis problem: count how often each digit (0–9) occurs and return the digit with the smallest frequency among those present.

Approach 1: Repeated Scan (Brute Force) (Time: O(10n), Space: O(1))

The straightforward way is to check each digit from 0 to 9 and count how many times it appears in the number. For every candidate digit, iterate through the digits of the number and increment a counter whenever a match occurs. Track the smallest count seen so far and update the result accordingly. Since you perform a full scan of the digits up to 10 times, the runtime becomes O(10n), which simplifies to O(n). Space usage stays O(1) because only a few counters are stored.

Approach 2: Counting / Frequency Table (Time: O(n), Space: O(1))

The optimal solution builds a frequency table in a single pass. Iterate through the digits of the number (or its string representation) and increment the count for each digit. A fixed-size array of length 10 works well because digits range from 0 to 9. After building the frequency table, scan the array to find the smallest non‑zero frequency and return the corresponding digit. This approach runs in O(n) time for the initial pass and O(10) for the final scan, which is effectively constant.

This technique is a classic application of counting and hash table style frequency tracking. Because the domain of digits is fixed and small, the memory footprint remains constant at O(1). Many interview problems involving digit statistics or character frequency rely on the same idea.

Implementation details are straightforward. Convert the number to a sequence of digits, update counts while iterating, and then compute the minimum frequency among digits that appear at least once. If multiple digits share the same minimum frequency, returning the smallest digit is a common tie-breaking rule. The algorithm relies only on simple iteration and arithmetic, making it ideal for problems tagged with array and math.

Recommended for interviews: Interviewers expect the frequency counting approach. The brute force scan demonstrates basic reasoning, but building a frequency table in one pass shows that you recognize the pattern of counting occurrences efficiently. This pattern appears frequently in string and digit problems, so mastering it saves time during interviews.

Solution

We use an array cnt to count the frequency of each digit. We iterate through each digit of the number n and update the cnt array.

Then, we use a variable f to record the current lowest frequency among the digits, and a variable ans to record the corresponding digit.

Next, we iterate through the cnt array. If 0 < cnt[x] < f, it means we have found a digit with a lower frequency, so we update f = cnt[x] and ans = x.

After the iteration, we return ans as the answer.

The time complexity is O(log n), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Repeated Scan (Brute Force)O(10n) ≈ O(n)O(1)When implementing a quick baseline or demonstrating the basic logic of counting digit occurrences.
Frequency Array / CountingO(n)O(1)General case and interview-preferred solution. Single pass counting followed by a constant-time scan of digits 0–9.

Video Solution

3663. Find The Least Frequent Digit (Leetcode Easy)Programming Live with Larry219 views views

Watch 3 more video solutions →

Frequently Asked Questions

Is Find The Least Frequent Digit easy or hard?
Find The Least Frequent Digit is typically categorized as an Easy problem. The main idea is straightforward frequency counting, a common beginner-friendly technique used in array, math, and hash table problems.
Find The Least Frequent Digit Python/Java solution
In Python or Java, convert the number to a string or repeatedly extract digits using modulo operations. Maintain a frequency array of size 10, update counts while iterating, and finally scan the array to return the digit with the minimum non-zero frequency. The implementation runs in O(n) time.
How to solve Find The Least Frequent Digit in O(n)?
Traverse the digits once and maintain a frequency array of size 10. Increment the counter for each digit encountered. After the pass, iterate through the array to find the digit with the smallest non-zero frequency. This guarantees O(n) time and constant extra space.
What is the best approach for Find The Least Frequent Digit?
The most efficient approach uses a counting array to track how many times each digit (0–9) appears. Iterate through the digits once to build the frequency table, then scan the table to find the smallest non-zero count. This runs in O(n) time with O(1) space because the digit range is fixed.
Is Find The Least Frequent Digit asked at Google/Amazon/Meta?
Digit frequency and counting problems appear frequently in coding interviews at companies like Amazon, Google, and Meta. While the exact problem title may vary, the underlying concept—tracking frequencies using arrays or hash maps—is a common interview pattern.
What data structure is used in Find The Least Frequent Digit?
A fixed-size array of length 10 is the most common structure because digits range from 0 to 9. Some implementations also use a hash table or map for counting, but an array is faster and uses constant memory.
What is the time complexity of Find The Least Frequent Digit?
The optimal solution runs in O(n) time where n is the number of digits in the input. One pass counts digit frequencies and a second constant-size scan (0–9) finds the minimum frequency. Space complexity remains O(1) due to the fixed-size frequency array.

Ready to solve this problem?

Practice Find The Least Frequent Digit with our built-in code editor and test cases.

Practice on FleetCode