Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Remove All Occurrences of a Digit

Basic Acceptance 78.62% Points 10

You are given a non-negative integer n and a digit d. Your task is to return the number formed by removing every occurrence of d, while preserving the order of the remaining digits.

If removing the digit leaves leading zeros, they should not appear in the final number. If all digits are removed, return 0.

Examples
Example 1

Input: n = 12321, d = 2
Output: 131
Explanation: Removing both occurrences of 2 leaves the digits 1, 3, and 1.

Example 2

Input: n = 5005, d = 0
Output: 55
Explanation: Removing both occurrences of 0 leaves the digits 5 and 5.

Example 3

Input: n = 1234, d = 9
Output: 1234
Explanation: The digit 9 does not appear in the number, so the original number remains unchanged.

Constraints
  • 0 <= n <= 1018
  • 0 <= d <= 9
Topics
Data Structures Basics Array String
Companies
Apple J.P. Morgan ServiceNow
Added 31 Oct 2026 78.62% acceptance
Ln 1, Col 1Sign in to save your code
1234 2
134