158. Remove Duplicate Characters from String

EasyStringString

Given a string s, remove all duplicate characters so that each character appears only once.

The relative order of the first occurrence of each character must be preserved.

Return the resulting string.

Input: A single string s.

Output: A string with all duplicate characters removed, preserving first-occurrence order.

Examples

Example 1
Input: programming
Output: progamin
Explanation: p(1st),r(1st),o(1st),g(1st → 2nd skip),r(skip),a(1st),m(1st → 2nd skip),i(1st),n(1st) → 'progamin'
Example 2
Input: banana
Output: ban
Explanation: b(keep), a(keep), n(keep), a(dup skip), n(dup skip), a(dup skip) → 'ban'
Example 3
Input: abcd
Output: abcd
Explanation: No duplicates exist; string is returned unchanged.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →