Thursday, December 26, 2024
Google search engine
HomeLanguagesPython – Find all duplicate characters in string

Python – Find all duplicate characters in string

Given a string, find all the duplicate characters which are similar to each other. Let us look at the example. 

Examples:

Input : hello
Output : l

Input : Lazyroarforgeeeks
Output : e g k s

Naive approach:

The idea is to use a dictionary to keep track of the count of each character in the input string. The program iterates through the string and adds each character to the dictionary, incrementing the count if the character is already present in the dictionary. After iterating through the string, the program then iterates through the dictionary to find characters with a count greater than 1, indicating that they are duplicates. These duplicate characters are stored in a list and returned as the output.

Implementation:

Python3




def duplicate_characters(string):
    # Create an empty dictionary
    chars = {}
 
    # Iterate through each character in the string
    for char in string:
        # If the character is not in the dictionary, add it
        if char not in chars:
            chars[char] = 1
        else:
            # If the character is already in the dictionary, increment the count
            chars[char] += 1
 
    # Create a list to store the duplicate characters
    duplicates = []
 
    # Iterate through the dictionary to find characters with count greater than 1
    for char, count in chars.items():
        if count > 1:
            duplicates.append(char)
 
    return duplicates
 
# Test cases
print(duplicate_characters("neveropen"))


Output

['g', 'e', 'k', 's']

Time complexity: O(n), where n is the length of the input string. 
Auxiliary space: O(k), where k is the number of distinct characters in the input string.

We have discussed a solution in the below post. Print all the duplicates in the input string We can solve this problem quickly using the python Counter() method. 

The approach is very simple. 

  1. Create a dictionary using the 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 values greater than 1. 

Python




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)
 
    # Finding no. of  occurrence of a character
    # and get the index of it.
    for letter, count in WC.items():
        if (count > 1):
            print(letter)
 
 
# Driver program
if __name__ == "__main__":
    input = 'neveropen'
    find_dup_char(input)


Output

e
g
k
s

Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(n) // since we are creating a dictionary and at worst case all elements will be stored inside it.

Approach : Using count() method

Python3




def find_dup_char(input):
    x=[]
    for i in input:
        if i not in x and input.count(i)>1:
            x.append(i)
    print(" ".join(x))
 
# Driver program
if __name__ == "__main__":
    input = 'neveropen'
    find_dup_char(input)


Output

g e k s

Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(n) // since we are using an extra list and in the worst case all elements will be stored inside it.

Approach : Using filter() method

Python




def find_dup_char(input):
    x = filter(lambda x: input.count(x) >= 2, input)
    print(' '.join(set(x)))
 
 
# Driver Code
if __name__ == "__main__":
    input = 'neveropen'
    find_dup_char(input)


Output

k e s g

Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(n)// since we are using a set to store all the values and in the worst case all elements will be stored inside it.

Using sets: 

Algorithm:

  • Create two empty sets, one to store unique characters and one to store duplicate characters.
  • Iterate through each character in the string.
  • If the current character is already in the unique_chars set, it is a duplicate, so add it to the duplicate_chars set. 
  • Otherwise, add it to the unique_chars set.
  • Return the duplicate_chars 
     

Python3




def find_duplicate_chars(string):
    # Create empty sets to store unique and duplicate characters
    unique_chars = set()
    duplicate_chars = set()
 
    # Iterate through each character in the string
    for char in string:
        # If the character is already in unique_chars, it is a duplicate
        if char in unique_chars:
            duplicate_chars.add(char)
        # Otherwise, add it to unique_chars
        else:
            unique_chars.add(char)
    return duplicate_chars
 
# Example usage:
print(find_duplicate_chars("neveropen")) # Output: ['e', 'g', 'k', 's']


Output

{'g', 's', 'e', 'k'}

Complexity Analysis:

The time complexity of this algorithm is O(n), where n is the length of the input string. 
The space complexity is also O(n), as the worst-case scenario is that all characters in the string are unique, and therefore all characters will be added to the char_set set.

Using functools.reduce method: 

Algorithm:

  • Initialize test string. 
  • Using reduce method on a string which iterates over each character of a string and performs a function on a string. 
  •  The function checks whether the character index from the left of the string and the right of the string is the same or not and whether it is already in the result or not.   
  • If any character satisfies the above condition then it is added to the result. 
  • Print the result. 

Python




from functools import reduce
 
def find_dup_char(input):
    x = reduce(lambda x, b: x + b if input.rindex(b) != input.index(b) and b not in x else x, input, '')
    print(x)
 
 
# Driver Code
if __name__ == "__main__":
    input = 'neveropen'
    find_dup_char(input)


Output

geks

Time Complexity: O(N)  where N is the length of the string
Auxiliary Space: O(M)  M is the length of the new string.t 

RELATED ARTICLES

Most Popular

Recent Comments