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)?
Example Input: nbHHkRvrXbvkn
Example Output: RrX
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)?
Example Input: nbHHkRvrXbvkn
Example Output: RrX
Here is a pretty fast one preserving order. But I'd be a little worried about how LINQ does Group and Where:
Edit: This one beats Luke's in some cases still slower than dtb's, but it preserves the order
Fastest as in fewest-lines-of-code:
Fastest as in fastest-performance would probably be something like this (does not preserve order):
Performance test
When in doubt -- test it :)
This one should be pretty quick (and it preserves the original order):
This preserves order and, based on my tests, is 4 times faster than using a HashSet. This assumes your character range is 0-255 but you can extend that easily. If you're planning on using this in a loop, move the
int[] c = new int[255];
out and in the function do anArray.Clear(c,0,255)
.This algorithm is general, can be applied to any language