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:
- Initialization: Start with an empty string
result
to hold unique characters. - For Loop: Iterate through each character in the input string.
- Check for Duplicates: Use
Contains()
to check if the character is already in theresult
string. - 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:
- 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.
- StringBuilder: This is used to efficiently build the resulting string without creating multiple intermediate string instances.
- 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 theStringBuilder
.
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:
Distinct()
: This LINQ method filters the characters to remove duplicates while maintaining their first occurrence.ToArray()
: Converts the distinct characters into an array.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