Replace All Occurrences of a Digit With Another Digit
Rebuild an integer with every occurrence of one digit replaced by another, using arithmetic only.
Peel off digits from the right with number % 10. If a digit equals the old digit, substitute the new digit. Rebuild the number by placing each (possibly substituted) digit with a running place multiplier: result += digit * place, then place *= 10. This preserves position while replacing every matching digit.
Problem Statement
Given a non-negative integer n, an old digit oldD and a new digit newD
(both 0-9), return the number formed by replacing every occurrence of oldD
with newD, keeping all other digits and their positions unchanged. Do not
convert the number to a String; use arithmetic only.
For example, replacing 2 with 5 in 12321 gives 15351. Replacing 0
with 9 in 1002 gives 1992.
Input: A non-negative integer n, an old digit oldD, and a new digit newD (each 0-9).
Output: The integer formed by replacing every oldD in n with newD.
Examples
Input: n = 12321, oldD = 2, newD = 5
Output: 15351Both 2s become 5s while the 1s and 3 stay in place.
Input: n = 1002, oldD = 0, newD = 9
Output: 1992Each 0 is replaced by 9, giving 1, 9, 9, 2.
Constraints
0 <= n <= 2,147,483,647 (fits in a 32-bit int)0 <= oldD <= 9 and 0 <= newD <= 9No String conversion; arithmetic only
Think Before You Code
Reveal the questions to ask yourself first
- How do you keep each surviving digit in its original position while rebuilding?
- When a digit matches the old digit, what do you substitute before placing it?
- How does a place multiplier help you reassemble the number in order?
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
Rebuild the number digit by digit from the right, substituting on the way.
- Start
result = 0andplace = 1. - Handle
n == 0directly: it is a single digit 0, which becomesnewDifoldD == 0, else stays 0. - While
nis not 0:digit = n % 10.- If
digit == oldD, setdigit = newD. result += digit * placeandplace *= 10.n = n / 10.
- Return
result.
Because every digit — replaced or not — advances the place multiplier, positions are preserved exactly.
Dry Run
Walk through the example step by step
Replacing 2 with 5 in n = 12321:
step | n | digit | after swap | result | place
-----+-------+-------+------------+--------+-------
1 | 12321 | 1 | 1 | 1 | 10
2 | 1232 | 2 | 5 | 51 | 100
3 | 123 | 3 | 3 | 351 | 1000
4 | 12 | 2 | 5 | 5351 | 10000
5 | 1 | 1 | 1 | 15351 | 100000
end | 0 | — | — | 15351 |
Solution
Reveal the full Java solution
public class ReplaceDigit {
public static int replace(int n, int oldD, int newD) {
if (n == 0) return oldD == 0 ? newD : 0;
long result = 0;
long place = 1;
while (n != 0) {
int digit = n % 10;
if (digit == oldD) digit = newD;
result += (long) digit * place;
place *= 10;
n /= 10;
}
return (int) result;
}
public static void main(String[] args) {
System.out.println(replace(12321, 2, 5)); // 15351
System.out.println(replace(1002, 0, 9)); // 1992
}
}
Every digit advances the place multiplier, whether or not it was substituted, so
the reconstructed number keeps each digit in its original column. A long
accumulator keeps the intermediate arithmetic safe for the largest 32-bit inputs.
O(d) where d is the number of digitsSpace: O(1)Common Mistakes
- Advancing the place multiplier only for replaced digits, which shifts the untouched digits out of position.
- Using result = result * 10 + digit, which reverses the number as it rebuilds.
Edge Cases to Test
- n = 0 with oldD = 0 returns newD; otherwise returns 0.
- Replacing a leading digit with 0 (like 2->0 in 234) yields a smaller number with a dropped leading zero (034 becomes 34).
- If oldD never appears, the number is returned unchanged.
Interview Follow-Ups
- How would you replace only the first occurrence of the digit?
- How would you swap two digits with each other in a single pass?
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 →