Write a Python program find duplicate character from a string.

How to find duplicate characters from a string in Python. Write a Python program to find duplicate characters from a string.

How to find duplicate characters from a string?

Duplicate characters are characters that appear more than once in a string. Printing duplicate characters in a string refers that we will print all the characters which appear more than once in a given string including space.

What are the duplicate characters from a string?

Input: hello welcome to Codebun
Output: the duplicate character in hello welcome to Codebun is
[‘ ‘, ‘e’, ‘c’, ‘o’]

Algorithm to find duplicate characters from a string:

• Input a string from the user.
• Initialize a variable with a blank array.
• Iterate the string using for loop and using if statement checks whether the character is repeated or not.
• On getting a repeated character add it to the blank array.
• Print the array.

Python program to find duplicate characters from a string.

string = input("enter the string whose duplicate characters you want to find:")

def duplicates_char(s):
    elements = {}
    for char in s:
        if elements.get(char,None) != None:
            elements[char]+=1
        else:
            elements[char] = 1
    return [k for k,v in elements.items() if v>1]
print("the duplicate character in",string,"is")
print(duplicates_char(string))

Output: Printing the duplicate characters in a string.