AI Features

Remove Duplicates from a String

Remove duplicate characters from a string, which is passed by reference.

Statement

Given a string that contains duplicate occurrences of characters, remove the duplicate occurrences such that every character in the string appears only once.

g a abbabcddbabcdeedebc b abcde a->b

Example

Sample input

abbabcddbabcdeedebc

Expected output

abcde

Try it yourself

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
void RemoveDuplicates(char* str){
//TODO: Write - Your - Code
}

Solution 1

  • We keep two pointers or indices, one for the current reading position and one for the current writing position.
  • Whenever we encounter the first occurrence of a character, we add it to the HashSet.
  • Any character already existing in the HashSet is skipped on any subsequent occurrence.

Below is an overview of the ...

Ask