Remove Duplication From String Using HashSet

Introduction

In this blog, we will learn about how we can remove duplication from the string using HashSet.

Given below code snippet will help you to understand how we will remove the duplications from String.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpAdvanncedProgramming
{
    public class RemoveDuplicationFromStringUsingLinq
    {
        public void RemoveDuplications()
        {
			try
			{
                string InitString = "Sardar Mudassar Ali";
                Console.WriteLine("Initial String: " + InitString);
                var uniquechar = new HashSet<char>(InitString);
                Console.Write("New String after removing duplicates: ");
                foreach (char c in uniquechar)
                    Console.Write(c);
            }
			catch (Exception ex)
			{
                Console.WriteLine("Error Details" + ex.Message);
			}
            finally {
                 Console.ReadKey();
              }
        }
    }
}

Call the method in the Main Class 

using CSharpAdvanncedProgramming;
RemoveDuplicationFromStringUsingLinq rd = new RemoveDuplicationFromStringUsingLinq();
rd.RemoveDuplications();

Program Output

Remove the Duplication From the String Using HashSet