---
title: "How to remove duplicate character from a given string?"  
description: "How to remove duplicate character from a given string?"  
author: "Revati S Misra"  
published: 2023-02-01  
updated: 2023-06-15  
canonical: https://www.mindstick.com/forum/157367/how-to-remove-duplicate-character-from-a-given-string  
category: "c#"  
tags: ["c#", "java", "programming language"]  
reading_time: 3 minutes  

---

# How to remove duplicate character from a given string?

How to [remove duplicate](https://www.mindstick.com/forum/157765/how-to-remove-duplicate-rows-in-sql) [characters](https://answers.mindstick.com/qa/42112/who-is-the-originator-of-avengers-characters) from a [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) which [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) entered?

## Replies

### Reply by Aryan Kumar

Sure, here is a C# code that removes duplicate characters from a given string:

C#

```plaintext
using System;

public class RemoveDuplicateCharacters
{
    public static string RemoveDuplicates(string input)
    {
        var seen = new HashSet<char>();
        var output = new StringBuilder();

        foreach (var ch in input)
        {
            if (!seen.Contains(ch))
            {
                seen.Add(ch);
                output.Append(ch);
            }
        }

        return output.ToString();
    }

    public static void Main(string[] args)
    {
        string input = "hello world";
        string output = RemoveDuplicates(input);
        Console.WriteLine(output);
    }
}
```

This code works by first creating a HashSet object to store the unique characters in the input string. Then, it creates a StringBuilder object to store the output string. Finally, it iterates through the input string and adds each unique character to the output string.

Here is an explanation of how the code works:

- The using System; statement imports the System namespace, which contains the HashSet and StringBuilder classes.
- The public class RemoveDuplicateCharacters statement defines a class called RemoveDuplicateCharacters.
- The public static string RemoveDuplicates(string input) method defines a method called RemoveDuplicates that takes a string as input and returns a string with the duplicate characters removed.
- The var seen = new HashSet<char>(); statement creates a HashSet object called seen.
- The var output = new StringBuilder(); statement creates a StringBuilder object called output.
- The foreach (var ch in input) statement iterates through the input string.
- The if (!seen.Contains(ch)) statement checks if the current character is not already in the seen HashSet.
- If the current character is not already in the seen HashSet, the seen.Add(ch) statement adds it to the seen HashSet.
- The output.Append(ch) statement appends the current character to the output StringBuilder.
- The return output.ToString(); statement returns the output StringBuilder as a string.
- The public static void Main(string[] args) statement defines a method called Main that is the entry point for the program.
- The string input = "hello world"; statement declares a string variable called input and initializes it to "hello world".
- The string output = RemoveDuplicates(input); statement calls the RemoveDuplicates method and stores the result in a string variable called output.
- The Console.WriteLine(output); statement prints the value of the output string to the console.
- The }``` statement closes theMain` method.
- The }``` statement closes theRemoveDuplicateCharacters` class.

I hope this helps!

### Reply by Rahul Roi

```cs
using System;
public class HelloWorld
{
   public static void Main(string[] args)
   {
       Console.WriteLine (RemoveDuplicateCaracters('akjsdhfkjahsdhhhhhhhhhhhhhhh)'));
   }
   static string RemoveDuplicateCaracters(string str)
   {
       string New_String = string.Empty;
       for (int i = 0; i < str.Length; i++)
       {
           if (!New_String.Contains(str[i]))
           {
               New_String += str[i];
           }
       }
       return New_String;
   }
}
```

### Reply by Tehran Noorani

There is the following simple C# program to [remove](https://yourviews.mindstick.com/story/4554/8-harmful-weeds-to-remove-from-garden) [duplicate](https://www.mindstick.com/forum/160911/sql-query-to-find-duplicate-records-in-a-table-in-sql-server) characters from a given string.

```plaintext
using System;
namespace Test
{
 class RepeatCharacter
 {
   static void Main(string[] args)
   {
    removeduplicate('Mississippi');
   }
   static void removeduplicate(string str)
    {
        string res = string.Empty;
        for (int i = 0; i < str.Length; i++)
        {
            if (!res.Contains(str[i]))
            {
                res += str[i];
            }
        }
        Console.WriteLine(res);
    }
 }
}

Output:  Misp
```


---

Original Source: https://www.mindstick.com/forum/157367/how-to-remove-duplicate-character-from-a-given-string

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
