Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Remove Duplicate Digits While Preserving Order

Basic Acceptance 78.19% Points 10.00

You are given a non-negative integer n. Your task is to return the number formed by keeping only the first occurrence of each digit, while preserving the original order of the digits.

Any digit that appears again after its first occurrence must be removed. The remaining digits must stay in the same relative order as in the original number.

Examples
Example 1

Input: n = 1122334
Output: 1234
Explanation: Only the first occurrence of each digit is kept, producing 1234.

Example 2

Input: n = 122333
Output: 123
Explanation: Repeated occurrences of 2 and 3 are removed, leaving their first appearances in the original order.

Example 3

Input: n = 1234
Output: 1234
Explanation: Every digit is distinct, so no digits need to be removed.

Constraints
  • 0 = n = 10¹8
Companies
Apple J.P. Morgan ServiceNow
Topics
Data Structures Basics
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