Remove Duplicate characters from String in C#
- The string may have two or more same characters in it but we want it to have only one. So let’s look at an example to understand it better.
class RemoveDuplicate
{
static void Main(string[] args)
{
string myStr = "kkllmmnnoo";
Console.WriteLine("Initial String: " + myStr);
var unique = new HashSet<char>(myStr);
Console.Write("New String after removing duplicates: ");
foreach (char c in unique)
Console.Write(c);
Console.ReadLine();
}
}
No comments