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.
Examples:
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' find_dup_char(input) |
Output:
e g k s
Recommended Posts:
- Python Counter| Find duplicate rows in a binary matrix
- Python Dictionary to find mirror characters in a string
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
- Dictionary and counter in Python to find winner of election
- Python Counter to find the size of largest subset of anagram words
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Python | Find keys with duplicate values in dictionary
- Iterate over characters of a string in Python
- Python | Extract only characters from given string
- Python | Ways to check string contain all same characters
- Python | Splitting string to list of characters
- Python | Splitting string to list of characters
- Python | Return lowercase characters from given string
- Concatenated string with uncommon characters in Python
- Check if both halves of the string have same set of characters in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.



