C# program to remove duplicate characters from a string

C# program to remove duplicate characters from a string:

In this post, we will learn how to remove all duplicate characters from a string in C#. The program will take one string as input from the user, remove all duplicate characters from the string and print the final result string.

String is immutable. We can’t modify a string directly. So, we need to create one different string using the character of the string.

The easiest way to solve it is by using a loop. We can loop through the characters of a string one by one and build one new string that will include only the unique characters. The drawback of this method is that we need to iterate through the string characters.

We can also use a HashSet. Since, HashSet can’t hold unique characters, it will remove all duplicates from the string characters if we create one HashSet with the string. We can iterate through its elements and rebuild one new string that will hold only unique characters.

Also, LINQ provides one method called Distinct that can be used to remove all duplicate characters from a string.

In this post, I will show you all these three methods with examples for each.

Method 1: By using a loop:

Let’s solve this by using a loop first. This program will iterate through the characters of the string one by one and build one new string. If it finds any character, which is already added to the new string, it will avoid that.

Below is the complete program:

using System;
					
public class Program
{
	public static void Main()
	{
		String inputStr, resultStr = String.Empty;
		
		Console.WriteLine("Enter a string:");
		inputStr = Console.ReadLine();
		
		for(int i = 0; i < inputStr.Length; i++){
			if(!resultStr.Contains(inputStr[i]+"")){
				resultStr += inputStr[i];
			}
		}
		
		Console.WriteLine("Final String: "+resultStr);
	}
}

Here,

  • inputStr is the string variable to hold the user input string and resultStr is the string variable to hold the final string.
  • It takes one string as input from the user and stores it in inputStr.
  • Then it iterates through the characters of the string inputStr one by one and creates the new string resultStr.
  • It checks if the current character in inputStr is in resultStr or not. If not, it adds it to resultStr.
  • Finally, resultStr holds the required string.

It will give one output as like below:

Enter a string:
Hello World
Final String: Helo Wrd

Method 2: By using a HashSet:

We can also do this by using a HashSet. Below is the complete program:

using System;
using System.Collections.Generic;
					
public class Program
{
	public static void Main()
	{
		String inputStr, resultStr = String.Empty;
		
		Console.WriteLine("Enter a string:");
		inputStr = Console.ReadLine();
		
		var hashSet = new HashSet<char>(inputStr);
		
		foreach(char c in hashSet){
			resultStr += c;
		}
		
		Console.WriteLine("Final String: "+resultStr);
	}
}

Here,

  • It is creating a HashSet of characters by passing the string user has entered. It will remove all duplicate characters.
  • Using a foreach loop, it is iterating through the characters in the hashset one by one and building the final string resultStr.

If you run this program, it will give a similar output.

Method 3: By using LINQ:

LINQ Distinct method is another way to remove duplicate characters from a string. It just take one line to remove all duplicate characters and to create a new array:

using System;
using System.Linq;
					
public class Program
{
	public static void Main()
	{		
		Console.WriteLine("Enter a string:");
		String inputStr = Console.ReadLine();
		
		String resultStr = new String(inputStr.Distinct().ToArray());
		
		Console.WriteLine("Final String: "+resultStr);
	}
}

This program will print similar output as the above programs.

You might also like: