Find Duplicate Characters in a String (2 ways)

By Lenin Mishra

If you prefer videos over text, click here.

Problem

Given a string with length > 0, write a function find_duplicates() to find all the duplicate characters in a string.

Example -

  1. find_duplicates(‘Hello’) - [‘l’]
  2. find_duplicates(‘Hippopotamus’) - [‘p’, ‘o’]
  3. find_duplicates(‘Python’) - [] (An empty list)

We are going to discuss 2 ways of solving this question.

The first way is a very generic python code that loops over all the elements in the string and stores the number of times each element occurs. It’s simple, but effective.

# Method 1

def find_duplicates(s):
    elements = {}
    for char in s:
        if elements.get(char,None) != None:
            elements[char]+=1
        else:
            elements[char] = 1
    return [k for k,v in elements.items() if v>1]

print(find_duplicates("Hello"))
>>> ['l']
print(find_duplicates("Hippopotamus"))
>>> ['p', 'o']
print(find_duplicates("Python"))
>>> []

The second way is by using the collections library. Specifically, the Counter method. This method reduces the above code to merely a few lines.

Watch how !!

# Method 2

from collections import Counter

def find_duplicates(s):
    elements = Counter(s)
    return [k for k,v in elements.items() if v>1]

print(find_duplicates("Hello"))
>>> ['l']
print(find_duplicates("Hippopotamus"))
>>> ['p', 'o']
print(find_duplicates("Python"))
>>> []

With just 2 lines of code, we were easily able to achieve our objective.

Let me know if you have a better way of doing this, through the comments.

Don't forget to subscribe to the Pydrools Newsletter