Fastest way to implement Duplicate Character Removal in String (C#)

前端 未结 5 694
说谎
说谎 2020-12-14 12:43

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)?

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-14 12:53

    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 an Array.Clear(c,0,255).

    
            private static string RemoveDuplicates(string s)
            {
                int[] c = new int[255];
                for (int i = 0; i < s.Length; i++)
                {
                    c[s[i]]++;
                }
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < s.Length; i++)
                {
                    if (c[s[i]] == 1) sb.Append(s[i]);
                }
                return sb.ToString();
            }
    

提交回复
热议问题