Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – Find all duplicate characters in string

  • Difficulty Level : Easy
  • Last Updated : 19 Feb, 2021

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

Let’s look at the example.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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
Recommended Articles
Page :

Start Your Coding Journey Now!