MCPcopy
hub / github.com/keon/algorithms / delete_reoccurring_characters

Function delete_reoccurring_characters

algorithms/string/delete_reoccurring.py:17–36  ·  view source on GitHub ↗

Remove duplicate characters, keeping only the first occurrence of each. Args: string: The input string to process. Returns: A new string with duplicate characters removed. Examples: >>> delete_reoccurring_characters("aaabcccc") 'abc'

(string: str)

Source from the content-addressed store, hash-verified

15
16
17def delete_reoccurring_characters(string: str) -> str:
18 """Remove duplicate characters, keeping only the first occurrence of each.
19
20 Args:
21 string: The input string to process.
22
23 Returns:
24 A new string with duplicate characters removed.
25
26 Examples:
27 >>> delete_reoccurring_characters("aaabcccc")
28 'abc'
29 """
30 seen_characters: set[str] = set()
31 output_string = ""
32 for char in string:
33 if char not in seen_characters:
34 seen_characters.add(char)
35 output_string += char
36 return output_string

Callers 1

Calls 1

addMethod · 0.80

Tested by 1