Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Remove Duplicates From A String

Easy Acceptance 61.68% Points 20

Given a string str that may contain both lowercase and uppercase letters, your task is to remove all duplicate characters from str while preserving the order of their first occurrence.

Examples
Example 1

image Input: "HappyProgramming"

Output: "HapyProgrmin"

Explanation: The resultant string is formed by retaining the first occurrence of each character.

Example 2

image Input: "aAaAaA"

Output: "aA"

Explanation: The first occurrence of a and A is preserved, and the duplicates are removed.

Constraints
  • 1 <= N <= 10^5
  • String contains uppercase and lowercase English letters.
Hints
  • Expected Time Complexity: O(N)
  • Expected Space Complexity: O(N)
Topics
String Stack
Companies
Amazon Microsoft Samsung Adobe TCS Intuit
Added 09 Aug 2026 61.68% acceptance
Ln 1, Col 1Sign in to save your code
HappyProgramming
HapyProgmin