Find duplicate characters in a string python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    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 : geeksforgeeeks
    Output : e g k s

    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]:

        WC = Counter[input]

        for letter, count in WC.items[]:

            if [count > 1]:

                print[letter]

    if __name__ == "__main__":

        input = 'geeksforgeeks'

        find_dup_char[input]

    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]]

    if __name__ == "__main__":

        input = 'geeksforgeeks'

        find_dup_char[input]


    In this tutorial, we are going to learn how to find all duplicate values in a string. We can do it in different ways in Python. Let's explore them one by one.

    The aim of the program we are going to write is to find the duplicate characters present in a string. For example, we have a string tutorialspoint the program will give us t o i as output. In simple words, we have to find characters whose count is greater than one in the string. Let's see.

    Scratch Programs

    Writing programs without using any modules. We can use different methods of Python to achieve our goal. First, we will find the duplicate characters of a string using the count method. Let's see the procedure first.

    • Initialize a string.
    • Initialize an empty list
    • Loop over the string.
      • Check whether the char frequency is greater than one or not using the count method.
    If greater than one check whether it's present in the list or not.
    If not present append to the list
    • Print the characters

    Example

    ## initializing string
    string = "tutorialspoint"
    ## initializing a list to append all the duplicate characters
    duplicates = []
    for char in string:
       ## checking whether the character have a duplicate or not
       ## str.count[char] returns the frequency of a char in the str
       if string.count[char] > 1:
       ## appending to the list if it's already not present
       if char not in duplicates:
       duplicates.append[char]
    print[*duplicates]

    If you run the above program, you will get the following results.

    Output

    t o i

    Now we will find the duplicate characters of string without any methods. We are going to use the dictionary data structure to get the desired output. Let's see the procedure first.

    • Initialize a string.
    • Initialize an empty dictionary
    • Loop over the string.
      • Check whether the char is already present in the dictionary or not
      • Initialize the count of the char to 1
    Increase the count

    Example

    ## initializing string
    string = "tutorialspoint"
    ## initializing a dictionary
    duplicates = {}
    for char in string:
       ## checking whether the char is already present in dictionary or not
       if char in duplicates:
          ## increasing count if present
          duplicates[char] += 1
       else:
          ## initializing count to 1 if not present
          duplicates[char] = 1
    for key, value in duplicates.items[]:
       if value > 1:
          print[key, end = " "]
    print[]

    If you run the above program,

    Output

    t o i

    Updated on 27-Aug-2019 12:37:28

    • Related Questions & Answers
    • Java program to find all duplicate characters in a string
    • Find All Duplicate Characters from a String using Python
    • Java Program to find duplicate characters in a String?
    • Java Program to Find the Duplicate Characters in a String
    • Program to find string after removing consecutive duplicate characters in Python
    • Program to find string after deleting k consecutive duplicate characters in python
    • Program to remove duplicate characters from a given string in Python
    • Python Program to find mirror characters in a string
    • C# Program to remove duplicate characters from String
    • Java program to delete duplicate characters from a given String
    • Python program to check if a string contains all unique characters
    • Find the smallest window in a string containing all characters of another string in Python
    • How to print duplicate characters in a String using C#?
    • C++ Program to Remove all Characters in a String Except Alphabets
    • Python program to Mark duplicate elements in string

    How do I find duplicate characters in a string?

    toCharArray[]; System. out. println["The string is:" + str]; The duplicate characters are found in the string using a nested for loop.

    How do you print repeated letters in a string in Python?

    Method 2:.
    Define a function which will take a word, m, n values as arguments..
    if M is greater than length of word. set m value equal to length of word..
    Now store the characters needed to be repeated into a string named repeat_string using slicing..
    Multiply the repeat_string with n..
    Now print the string..

    How do you find duplicate elements in Python?

    ALGORITHM:.
    STEP 1: Declare and initialize an array..
    STEP 2: Duplicate elements can be found using two loops. The outer loop will iterate through the array from 0 to length of the array. ... .
    STEP 3: If a match is found which means the duplicate element is found then, display the element..

    Chủ Đề