Remove Duplicate Characters
Given a string s, return a new string that contains each character of s exactly once, keeping only the first occurrence of every character and preserving the…
Given a string s, return a new string that contains each character of s exactly once, keeping only the first occurrence of every character and preserving the original left-to-right order. For example, the string "balloon" becomes "balon" because the second 'l' and second 'o' are duplicates of characters that already appeared earlier.
- `1 ≤ s.length ≤ 10^4` - `s` consists of lowercase English letters only. - The interactive visualizer accepts at most 26 characters; longer valid judge inputs are execution-only.
Example 1: input s = "character"; output "charte". Scanning left to right: c, h, a, r are new — kept. Second a and second c are duplicates — skipped. t and e are new — kept. Second r is a duplicate — skipped. Result: "charte".
Example 2: input s = "mississippi"; output "misp". First occurrences in order: m at index 0, i at index 1, s at index 2, p at index 8. All subsequent i, s, and p characters are duplicates and are discarded.
Example 3: input s = "abcabc"; output "abc". The first three characters a, b, c are each new and kept. The second a, b, c are all repeats of already-seen characters and are dropped.
Category: Strings. Topic: strings. Difficulty: Easy. Patterns: hashing.