0
0
CppProgramBeginner · 2 min read

C++ Program to Remove Duplicates from String

You can remove duplicates from a string in C++ by using a std::unordered_set to track seen characters and build a new string with only unique characters, like this: iterate over the original string, add unseen characters to the result, and skip duplicates.
📋

Examples

Inputhello
Outputhelo
Inputprogramming
Outputprogamin
Input
Output
🧠

How to Think About It

To remove duplicates from a string, think of checking each character one by one and remembering which characters you have already seen. If a character is new, add it to the result; if it is already seen, skip it. This way, you keep only the first occurrence of each character.
📐

Algorithm

1
Get the input string.
2
Create an empty set to store characters seen so far.
3
Create an empty string to store the result.
4
For each character in the input string:
5
Check if the character is in the set; if not, add it to the set and append it to the result string.
6
Return or print the result string.
💻

Code

cpp
#include <iostream>
#include <unordered_set>
#include <string>

int main() {
    std::string input = "programming";
    std::unordered_set<char> seen;
    std::string result;

    for (char c : input) {
        if (seen.find(c) == seen.end()) {
            seen.insert(c);
            result += c;
        }
    }

    std::cout << result << std::endl;
    return 0;
}
Output
progamin
🔍

Dry Run

Let's trace the input "programming" through the code

1

Start with empty set and result

seen = {}, result = ""

2

Process 'p'

'p' not in seen, add 'p' to seen and result seen = {'p'}, result = "p"

3

Process 'r'

'r' not in seen, add 'r' seen = {'p','r'}, result = "pr"

4

Process 'o'

'o' not in seen, add 'o' seen = {'p','r','o'}, result = "pro"

5

Process 'g'

'g' not in seen, add 'g' seen = {'p','r','o','g'}, result = "prog"

6

Process 'r'

'r' already in seen, skip seen = {'p','r','o','g'}, result = "prog"

7

Process 'a'

'a' not in seen, add 'a' seen = {'p','r','o','g','a'}, result = "proga"

8

Process 'm'

'm' not in seen, add 'm' seen = {'p','r','o','g','a','m'}, result = "progam"

9

Process 'm'

'm' already in seen, skip seen = {'p','r','o','g','a','m'}, result = "progam"

10

Process 'i'

'i' not in seen, add 'i' seen = {'p','r','o','g','a','m','i'}, result = "progami"

11

Process 'n'

'n' not in seen, add 'n' seen = {'p','r','o','g','a','m','i','n'}, result = "progamin"

12

Process 'g'

'g' already in seen, skip seen = {'p','r','o','g','a','m','i','n'}, result = "progamin"

CharacterSeen SetResult String
p{p}p
r{p,r}pr
o{p,r,o}pro
g{p,r,o,g}prog
r{p,r,o,g}prog
a{p,r,o,g,a}proga
m{p,r,o,g,a,m}progam
m{p,r,o,g,a,m}progam
i{p,r,o,g,a,m,i}progami
n{p,r,o,g,a,m,i,n}progamin
g{p,r,o,g,a,m,i,n}progamin
💡

Why This Works

Step 1: Use a set to track seen characters

A std::unordered_set stores characters already encountered, allowing quick checks to avoid duplicates.

Step 2: Build result with unique characters

Only characters not in the set are added to the result string, preserving the order of first appearance.

Step 3: Skip duplicates efficiently

If a character is already in the set, it is skipped, so duplicates are removed without extra loops.

🔄

Alternative Approaches

Using std::string and std::find
cpp
#include <iostream>
#include <string>

int main() {
    std::string input = "hello";
    std::string result;
    for (char c : input) {
        if (result.find(c) == std::string::npos) {
            result += c;
        }
    }
    std::cout << result << std::endl;
    return 0;
}
Simpler but slower for long strings because <code>find</code> scans the result string each time.
Using sorting and unique (modifies order)
cpp
#include <iostream>
#include <string>
#include <algorithm>

int main() {
    std::string input = "programming";
    std::sort(input.begin(), input.end());
    auto last = std::unique(input.begin(), input.end());
    input.erase(last, input.end());
    std::cout << input << std::endl;
    return 0;
}
Removes duplicates but changes character order by sorting.

Complexity: O(n) time, O(n) space

Time Complexity

The program loops through the string once, and each set insertion and lookup is O(1) on average, so total time is O(n).

Space Complexity

Extra space is used for the set and the result string, both up to size n in the worst case.

Which Approach is Fastest?

Using an unordered_set is faster than searching the result string each time, and preserves order unlike sorting.

ApproachTimeSpaceBest For
unordered_set trackingO(n)O(n)Fast removal preserving order
string find methodO(n^2)O(n)Simple code, small strings
sort + uniqueO(n log n)O(1)When order does not matter
💡
Use an unordered_set to track seen characters for fast duplicate removal while preserving order.
⚠️
Trying to remove duplicates by modifying the string while iterating without careful indexing causes errors.