In C#, what is the fastest way to detect duplicate characters in a String and remove them (removal including 1st instance of the duplicated character)?
This one should be pretty quick (and it preserves the original order):
public static string RemoveDuplicates(string source)
{
HashSet found = new HashSet();
HashSet dupes = new HashSet();
foreach (char c in source)
{
if (!found.Add(c))
{
dupes.Add(c);
}
}
StringBuilder sb = new StringBuilder(source.Length);
foreach (char c in source)
{
if (!dupes.Contains(c))
{
sb.Append(c);
}
}
return sb.ToString();
}