Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Remove All Occurrences of a Digit

Basic Acceptance 78.62% Points 10.00

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 = 10¹8
  • 0 = d = 9
Companies
Apple J.P. Morgan ServiceNow
Topics
Data Structures Basics Array String
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions