Remove Duplicate Characters

Medium~15 min

Given a string s, remove all duplicate characters so that each character appears only once. Preserve the order of first occurrence of each character.

For example, "abracadabra" becomes "abrcd" because those are the characters in the order they first appear.

Examples

Example 1
Input: s = "abracadabra"
Output: "abrcd"
Explanation: First occurrences in order: a(0), b(1), r(2), c(4), d(6). Subsequent duplicates are removed.
Example 2
Input: s = "hello"
Output: "helo"
Explanation: The second "l" is a duplicate and is removed.
Example 3
Input: s = "aabbcc"
Output: "abc"
Explanation: Each character appears twice. Only the first occurrence of each is kept.

Constraints

  • 0 <= s.length <= 10^4
  • s consists of printable ASCII characters
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run