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

前端 未结 5 687
说谎
说谎 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 13:19

    This algorithm is general, can be applied to any language

    1. create a map(HashTable) char->int that holds the count of each character found, initially empty
    2. scan the string once to populate the map.
    3. create a new empty string that'll hold the output, may be you'll need to use a StringBuilder.
    4. scan the string(orthe map, whichever is shorter) copying only characters which an occurrence of 1 to the output string/StringBuilder

提交回复
热议问题