How to Remove Duplicate Characters From a String in C#

How to Remove Duplicate Characters from a String in C#

In this article, I am going to discuss how to remove duplicate characters from a string in C# with some examples. Please read our previous article where we discussed How to Reverse Each Word in a Given String in C# with some examples. As part of this article, we are going to use the following three approaches to remove the duplicate characters from the given string C#.

  1. The simple way of Implementation to remove duplicate characters
  2. Using HashSet to Remove Duplicate Characters from a string
  3. Using LINQ to Remove Duplicate Characters from a string
Program Description:

Here, the user will input a string and that string may contain some characters multiple times. Our requirement is to have a character only once in the string. So here we need to remove the duplicate characters from the string. For better understanding, please have a look at the following diagram which shows the input and the expected output.

How to Remove Duplicate Characters from a String in C#

The Simple way of Implementation:

In the following program, we are looping through all the characters of the input and string and then checking whether that character is already there in the result string. If it is already there then we simply ignore it else we add that character to the end of the result string.

using System;
using System.Linq;

namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a String : ");
            string inputString = Console.ReadLine();
            string resultString = string.Empty;

            for (int i = 0; i < inputString.Length; i++)
            {
                if (!resultString.Contains(inputString[i]))
                {
                    resultString += inputString[i];
                }
            }
            Console.WriteLine(resultString);

            Console.ReadKey();
        }
    }
}

Here, we use the Contains method which will check whether a character is present or not.

Using HashSet to Remove Duplicate Characters:

In the following example, we use HashSet to map the string to char. This will remove the duplicate characters from a string.

using System;
using System.Collections.Generic;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a String : ");
            string inputString = Console.ReadLine();
            string resultString = string.Empty;

            var unique = new HashSet<char>(inputString);
            foreach (char c in unique)
            {
                resultString += c;
            }
            Console.WriteLine("After Removing Duplicates : " + resultString);

            Console.ReadKey();
        }
    }
}
Using LINQ to Remove Duplicate Characters From a String:

In the following example, first we are converting the string into a character array and then we are applying the LINQ Distinct method to remove the duplicate characters. Finally, we convert the character array into a string which should contain the unique characters.

using System.Linq;
using System;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a String : ");
            string inputString = Console.ReadLine();
            
            var uniqueCharArray = inputString.ToCharArray().Distinct().ToArray();
            var resultString = new string(uniqueCharArray);

            Console.WriteLine("After Removing Duplicates : " + resultString);
            Console.ReadKey();
        }
    }
}

Output:

Using LINQ to Remove Duplicate characters from a string in C#

In the next article, I am going to discuss how to find all possible substrings of a given string in C# with some examples. I hope now you understood How to remove duplicate characters from a string with different ways.