FastPrepRemove Duplicate Characters

Remove Duplicate Characters

WhatNot logoWhatNot● EasyFULLTIMEPHONE SCREEN
Learn

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) → String

Examples

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.
  • s contains only lowercase English letters.

More WhatNot problems

See WhatNot hiring insights
public String removeDuplicateCharacters(String s) {
    // write your code here
}
s"banana"
expected"ban"
Checking account…