Python program to find all duplicate characters in a string

This article teaches you how to write a python program to find all duplicate characters in a string.

Characters that repeat themselves within a string are referred to as duplicate characters. When we refer to printing duplicate characters in a string, we mean that we shall print every character, including spaces, that appears more than once in the string in question.

Input-Output Scenarios

Following is the input-output scenario to find all the duplicate characters in a string −

Input: TutorialsPoint
Output: t, o, i

As we can see, the duplicate characters in the given string "TutorialsPoint" are "t" with "3" repetitions, "o" with "2" repetitions and "i" with "2" reputations.


The following algorithm will search a string for duplicate characters −

  • Create a String and store it in a variable.

  • To find the duplicate characters, use two loops. A character will be chosen and the variable count will be set to 1 using the outer loop

  • To compare the selected character with the remaining characters in the string, an inner loop will be employed.

  • If a match is found, the count is raised by 1.

  • If a character's count is more than 1 once the inner loop is finished, there are duplicate characters in the string.

  • Print the character count and all the repeated characters.

  • End

We can implement the above algorithm in various ways let us see them one by one −

Using loops

For iterating repeatedly through a sequence, use a for loop. This functions more like an iterator method seen in other object-oriented programming languages and is less like the for keyword found in other programming languages.


Following is an example to find all the duplicate characters in a string using loops −

string = "Welcome to TutorialsPoint family"; print("All the duplicate characters in the string are: "); # Counting every characters of the string for s in range(0, len(string)): count = 1; for t in range(s+1, len(string)): if(string[s] == string[t] and string[s] != ' '): count = count + 1; # setting the string t to 0 to avoid printing the characters already taken string = string[:t] + '0' + string[t+1:]; # If the count is greater than 1, the character is considered as duplicate if(count > 1 and string[s] != '0'): print(string[s]," - ",count);


Following is an output of the above code −

All the duplicate characters in the string are: 
e  -  2
l  -  3
o  -  4
m  -  2
t  -  3
o  -  3
t  -  2
o  -  2
i  -  3
a  -  2
l  -  2
i  -  2

Using list using count() method

The frequency of a character or substring within a string can be counted using the count() function of the Python language. As a return value, it simply provides the count of occurrences.


Following is an example to find all the duplicate characters in a string using count() method −

# initializing the string str = "tutorialspoint" # initializing a list to add all the duplicate characters duplicate_char = [] for character in str: # check whether there are duplicate characters or not # returning the frequency of a character in the string if str.count(character) > 1: # append to the list if it is already not present if character not in duplicate_char: duplicate_char.append(character) print(*duplicate_char)


Following is an output of the above code −

t o i

Using Counter() method

Python's Counter subclass of dict is created specifically for counting hashable objects. It is a dictionary where numbers are the values and objects are the keys. You normally pass a sequence or iterable of hashable objects as an input to the class's constructor when using Counter.


Using the Counter() method, create a dictionary with strings as keys and frequencies as values. After that, create a temporary variable and print every index derived from keys with values greater than 1 as shown in the following example −

from collections import Counter def duplicate_character(input): # creating the dictionary by using counter method having strings as key and its frequencies as value string = Counter(input) # Find the number of occurrence of a character and getting the index of it. for char, count in string.items(): if (count > 1): print(char) # Driver code if __name__ == "__main__": input = 'TutorialsPoint' duplicate_character(input)


Following is an output of the above code −


Updated on: 26-Aug-2023

30K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started