Remove Duplicate Characters
Problem statement
Given a string s, remove repeated occurrences of each character so that every distinct character appears exactly once. Keep its first occurrence, preserving the order in which distinct characters first appear.
Duplicates may occur anywhere, not only next to each other. For example, abbaca becomes abc. Characters are not removed in pairs, and the output is not rearranged alphabetically.
Return the resulting string. An empty input produces an empty string.
Function
removeDuplicateCharacters(s: String) → StringExamples
Example 1
s = "banana"return = "ban"The first appearances are b, a, n; later copies are removed.
Example 2
s = "cbacdcbc"return = "cbad"Distinct characters first appear as c, b, a, d.
Example 3
s = ""return = ""There are no characters to retain.
Constraints
0 <= s.length <= 10000.scontains only lowercase English letters.