Python Counter| Find all duplicate characters in string

Given a string, find all the duplicate characters which are similar to each others.

Let’s look at the example.

Input : hello
Output : l

Input : geeksforgeeeks
Output : e g k s

We have discussed a solution in below post.
Print all the duplicates in the input string

We can solve this problem quickly using python Counter() method. Approach is very simple.

1) First create a dictionary using Counter method having strings as keys and their frequencies as values.
2) Declare a temp variable.
3) Print all the indexes from the keys which have value greater than 1.





from collections import Counter
def find_dup_char(input):
    # now create dictionary using counter method
    # which will have strings as key and their 
    # frequencies as value
    WC = Counter(input)
    j = -1
    # Finding no. of  occurrence of a character
    # and get the index of it.
    for i in WC.values():
        j = j + 1
        if( i > 1 ):
            print WC.keys()[j],
# Driver program
if __name__ == "__main__":
    input = 'geeksforgeeks'



e g k s
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.