Exercise 90

Create a Python program that identifies all characters appearing at least twice in a given string s. For instance, if the string is s = "Programming language", the program should return a list of such characters.

['r' , 'a' , 'g' , 'm']

Solution

def find_repeated_chars(s):
    repeated_chars = []
    seen_chars = set()

    for char in s:
        if char in seen_chars:
            repeated_chars.append(char)
        else:
            seen_chars.add(char)

    return list(set(repeated_chars))

# Example usage:
s = "Programming language"
print(find_repeated_chars(s))

 

Younes Derfoufi
www.my-courses.net

Leave a Reply