0
0
CsharpProgramBeginner · 2 min read

C# Program to Remove Duplicates from String

You can remove duplicates from a string in C# by using a HashSet to track seen characters and build a new string with only unique characters, like this: var seen = new HashSet(); var result = new string(input.Where(c => seen.Add(c)).ToArray());
📋

Examples

Inputhello
Outputhelo
Inputprogramming
Outputprogamin
Input
Output
🧠

How to Think About It

To remove duplicates from a string, think about checking each character one by one and remembering which characters you have already seen. If a character is new, keep it; if it appeared before, skip it. This way, you build a new string with 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 result string builder.
4
For each character in the input string:
5
Check if the character is not in the set.
6
If not, add it to the set and append it to the result.
7
Return the result string.
💻

Code

csharp
using System;
using System.Collections.Generic;
using System.Text;

class Program
{
    static void Main()
    {
        string input = "programming";
        string output = RemoveDuplicates(input);
        Console.WriteLine(output);
    }

    static string RemoveDuplicates(string input)
    {
        HashSet<char> seen = new HashSet<char>();
        StringBuilder result = new StringBuilder();

        foreach (char c in input)
        {
            if (seen.Add(c))
            {
                result.Append(c);
            }
        }

        return result.ToString();
    }
}
Output
progamin
🔍

Dry Run

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

1

Initialize

input = "programming", seen = {}, result = ""

2

Process 'p'

seen = {'p'}, result = "p"

3

Process 'r'

seen = {'p','r'}, result = "pr"

4

Process 'o'

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

5

Process 'g'

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

6

Process 'r' again

already in seen, skip

7

Process 'a'

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

8

Process 'm'

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

9

Process 'm' again

already in seen, skip

10

Process 'i'

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

11

Process 'n'

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

12

Process 'g' again

already in seen, skip

Character ProcessedSeen 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 HashSet to Track Characters

The HashSet stores characters already seen, so we can quickly check if a character is a duplicate.

Step 2: Build Result with Unique Characters

We add characters to the result only if they are not in the set, ensuring no duplicates appear.

Step 3: Return the Clean String

Finally, we convert the collected characters back to a string and return it as the result without duplicates.

🔄

Alternative Approaches

Using LINQ Distinct
csharp
using System;
using System.Linq;

class Program
{
    static void Main()
    {
        string input = "programming";
        string output = new string(input.Distinct().ToArray());
        Console.WriteLine(output);
    }
}
This method is concise and uses LINQ's Distinct to remove duplicates but preserves the original order of characters.
Using StringBuilder and Contains Check
csharp
using System;
using System.Text;

class Program
{
    static void Main()
    {
        string input = "programming";
        StringBuilder result = new StringBuilder();

        foreach (char c in input)
        {
            if (!result.ToString().Contains(c))
            {
                result.Append(c);
            }
        }

        Console.WriteLine(result.ToString());
    }
}
This approach is simple but less efficient because it checks the result string for each character, causing slower performance on long strings.

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

Time Complexity

The program loops through each character once, and HashSet operations like Add are O(1) on average, so total time is O(n) where n is string length.

Space Complexity

Extra space is used for the HashSet and the result string builder, both proportional to the number of unique characters, up to O(n).

Which Approach is Fastest?

Using HashSet is fastest for large strings due to O(1) lookups, while LINQ Distinct is concise and preserves order, and string Contains checks are slower.

ApproachTimeSpaceBest For
HashSet with StringBuilderO(n)O(n)Preserving order efficiently
LINQ DistinctO(n)O(n)Concise code, preserves order
StringBuilder with ContainsO(n²)O(n)Simple code, small strings only
💡
Use a HashSet to track seen characters for fast duplicate detection.
⚠️
Beginners often try to remove duplicates by nested loops, which is inefficient and complicated.