Remove Duplicate Characters From a String C#

This blog post provides various code examples to remove duplicate characters from a string in C#.

The below code example is very simple way to remove duplicates from a string using for loop. Here’s how you can do that:

 using System;  
 class Program  
 {  
   static void Main()  
   {  
     string inputString = "programming";  
            Console.WriteLine("Input string is '{0}'", inputString);  
     string resultString = RemoveDuplicates(inputString);  
      Console.WriteLine("Result after removing duplicates is '{0}'",resultString);  
   }  
   static string RemoveDuplicates(string input)  
   {  
     string result = string.Empty;  
     for (int i = 0; i < input.Length; i++)  
     {  
       // Check if the character is already in the result string  
       if (!result.Contains(input[i]))  
       {  
         result += input[i]; // Add it to the result if not a duplicate  
       }  
     }  
     return result;  
   }  
 }  

This method is straightforward and works well for small strings, though it may not be the most efficient for larger strings due to the repeated calls to Contains().

Explanation:

  1. Initialization: Start with an empty string result to hold unique characters.
  2. For Loop: Iterate through each character in the input string.
  3. Check for Duplicates: Use Contains() to check if the character is already in the result string.
  4. Append Unique Characters: If it’s not a duplicate, append it to the result.

Remove Duplicate Characters Using HashSet and StringBuilder

You can remove duplicate characters from a string in C# by using a combination of a HashSet and a StringBuilder. Here’s a simple example of how to do this:

 using System;  
 using System.Collections.Generic;  
 using System.Text;  
 class Program  
 {  
   static void Main()  
   {  
     string inputString = "programming";  
           Console.WriteLine("Input string is '{0}'", inputString);  
     string resultString = RemoveDuplicates(inputString);  
     Console.WriteLine("Result after removing duplicates is '{0}'",resultString);  
   }  
   static string RemoveDuplicates(string input)  
   {  
     HashSet<char> seen = new HashSet<char>();  
     StringBuilder sb = new StringBuilder();  
     foreach (char c in input)  
     {  
       if (seen.Add(c)) // Add returns false if the character is already in the set  
       {  
         sb.Append(c);  
       }  
     }  
     return sb.ToString();  
   }  
 }  

Explanation:

  1. HashSet: This data structure is used to keep track of characters that have already been seen. It allows for fast lookups to check if a character is a duplicate.
  2. StringBuilder: This is used to efficiently build the resulting string without creating multiple intermediate string instances.
  3. Loop through input: For each character in the input string, check if it has been seen before. If not, add it to both the HashSet and the StringBuilder.

This method preserves the order of first appearances of the characters in the original string.

Remove Duplicate Characters From a String using LINQ

Duplicate characters also can be remoded using the distinct function of LINQ.Here’s a concise way to achieve that:

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.Threading.Tasks;  
 namespace comparedelegate  
 {  
   class Program  
   {  
     static void Main(string[] args)  
     {  
       string inputString = "programming";  
       Console.WriteLine("Input string is '{0}'", inputString);  
       string resultString = string.Empty;  
       var uniqueCharArray = inputString.ToCharArray().Distinct().ToArray();  
       resultString = new string(uniqueCharArray);  
       Console.WriteLine("Result after removing duplicates is '{0}'", resultString);  
       Console.ReadLine();  
     }  
   }  
 }  

Explanation:

  1. Distinct(): This LINQ method filters the characters to remove duplicates while maintaining their first occurrence.
  2. ToArray(): Converts the distinct characters into an array.
  3. new string(...): Constructs a new string from the array of characters.

This approach is elegant and leverages LINQ for brevity. However, note that the order of characters will be the first occurrence in the input string.

Result
 Input string is 'programming'  
 Result after removing duplicates is 'progamin'  

Thanks

Kailash Chandra Behera

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

Previous Post Next Post

نموذج الاتصال