Skip to main content

How to Find Duplicate Characters in a String with Python

Identifying duplicate characters within a string is a common task in string processing and data validation.

This guide explores different methods to check if a character appears multiple times in a string using count(), set(), and for loops, with examples demonstrating the functionality and performance considerations of each method.

Checking for Specific Character Repetition​

Checking if a character appears twice​

To check if a specific character appears exactly twice, use the count() method:

my_str = 'tutorialreference.com'

if my_str.count('t') == 2:
print('The character appears twice in the string')
else:
print('The character does NOT appear twice in the string')

Output:

The character appears twice
note

The count() method directly returns the amount of occurrences of the substring provided as an argument.

Checking if a character appears multiple times​

You can adapt the approach by using a different comparison to see if the count is above a certain amount:

my_str = 'tutorialreference.com'

if my_str.count('e') > 1:
print('The character appears more than once in the string')
else:
print('The character does NOT appear more than once')

Output:

The character appears more than once

Checking for Any Repeated Characters​

Using set()​

To efficiently check if any character is repeated (without needing to know which character), compare the length of the string to the length of a set created from the string:

my_str = 'tutorial'
has_repeated_chars = len(set(my_str)) != len(my_str)
print(has_repeated_chars) # Output: True

my_str = 'abc'
has_repeated_chars = len(set(my_str)) != len(my_str)
print(has_repeated_chars) # Output: False
note

Sets only store unique items, so any duplicates are removed.

Using a for Loop​

A for loop offers another way to check, by counting the occurrences of each character. This approach also stops checking as soon as a repetition is found, improving efficiency:

my_str = 'tutorial'

def has_repeated_chars(string):
for char in string:
if string.count(char) > 1:
return True # Return immediately when a duplicate is found
return False # Return False only if the loop completes without finding duplicates

print(has_repeated_chars('tutorial')) # Output: True
print(has_repeated_chars('abc')) # Output: False

Finding All Duplicate Characters​

To find all the duplicate characters (not just check for any), iterate and keep track of characters already seen:

def find_duplicate_chars(string):
duplicate_chars = []
for char in string:
if string.count(char) > 1 and char not in duplicate_chars:
duplicate_chars.append(char)
return duplicate_chars

print(find_duplicate_chars('tutorial')) # Output: ['b']
print(find_duplicate_chars('abc ac')) # Output: ['a', 'c', ' ']
  • The loop checks if any element is repeated and if so, it adds it to the duplicate_chars list.
  • Note that the condition char not in duplicate_chars is necessary to avoid including the character multiple times in the result list.

Conclusion​

This guide presented various methods for identifying duplicate characters within strings in Python.

  • You learned how to use the count method to identify if a specific character appears a number of times, how to determine the presence of any duplicate characters using set(), and how to create a list of duplicate characters using a for loop.
  • The best method for your task depends on whether you need to check for a specific character, any duplicates, or find all duplicates.
  • The set() approach is generally the most efficient for simply checking for any duplicates due to its inherent uniqueness property.