Remove All Zeros From a Number
Rebuild an integer after dropping every digit that equals zero, using arithmetic only.
Peel off digits from the right with number % 10. When a digit is not zero, place it into the result using a running place multiplier (result += digit * place; place *= 10). Skip zeros entirely so they never take up a place. The result is the original number with every zero removed.
Problem Statement
Given a non-negative integer, return the number formed by removing every digit
that equals 0, keeping the order of the remaining digits. Do not convert the
number to a String; use arithmetic only.
For example, 1020304 becomes 1234. If a number is made only of significant
digits it is returned unchanged, and a number like 1000 collapses down to 1.
Input: A single non-negative integer n.
Output: The integer formed by deleting all zero digits from n.
Examples
Input: 1020304
Output: 1234The three interior zeros are dropped, leaving digits 1, 2, 3, 4 in order.
Input: 1000
Output: 1All three trailing zeros are removed, leaving just the leading 1.
Constraints
0 <= n <= 2,147,483,647 (fits in a 32-bit int)No String conversion; arithmetic only
Think Before You Code
Reveal the questions to ask yourself first
- How do you inspect one digit at a time from the right?
- When you keep a digit, how do you place it at the correct position in the answer?
- If you skip a zero, should the place value still advance?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Build the answer from the least significant digit up, advancing the place value only for digits you keep.
- Start
result = 0andplace = 1. - While
nis not 0:digit = n % 10.- If
digit != 0:result += digit * placeand thenplace *= 10. n = n / 10.
- Return
result.
Because place only grows when a non-zero digit is stored, the kept digits are
packed together with no gaps, exactly as if the zeros had been deleted.
Dry Run
Walk through the example step by step
Removing zeros from n = 1020304:
step | n | digit | kept? | result | place
-----+---------+-------+-------+--------+------
1 | 1020304 | 4 | yes | 4 | 10
2 | 102030 | 0 | no | 4 | 10
3 | 10203 | 3 | yes | 34 | 100
4 | 1020 | 0 | no | 34 | 100
5 | 102 | 2 | yes | 234 | 1000
6 | 10 | 0 | no | 234 | 1000
7 | 1 | 1 | yes | 1234 | 10000
end | 0 | — | — | 1234 |
Solution
Reveal the full Java solution
public class RemoveZeros {
public static int removeZeros(int n) {
long result = 0;
long place = 1;
while (n != 0) {
int digit = n % 10;
if (digit != 0) {
result += (long) digit * place;
place *= 10;
}
n /= 10;
}
return (int) result;
}
public static void main(String[] args) {
System.out.println(removeZeros(1020304)); // 1234
System.out.println(removeZeros(1000)); // 1
}
}
The place multiplier is the key idea: it advances only when a digit survives, so
the remaining digits are re-packed with no holes where the zeros used to be.
Using a long for the accumulator keeps the intermediate math safe even for the
largest 32-bit inputs.
O(d) where d is the number of digits (log10 n)Space: O(1)Common Mistakes
- Advancing the place multiplier even for skipped zeros, which reintroduces gaps and keeps the zeros.
- Rebuilding with result = result * 10 + digit, which reverses the digit order.
Edge Cases to Test
- A number of all zeros conceptually (like the input 0) returns 0.
- A number with no zeros such as 1234 is returned unchanged.
- Numbers ending in zeros like 1000 shrink dramatically to 1.
Interview Follow-Ups
- How would you remove every occurrence of an arbitrary digit, not just zero?
- How would you preserve the sign for negative inputs?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →