Python find duplicate characters in a string

This article will discuss different ways to remove duplicate characters from a string in Python.

Table Of Contents

  • Remove Duplicate Characters from String using set[] and sorted[]
  • Remove Duplicate Characters from String using OrderedDict
  • Remove Duplicate Characters from String using dict
  • Remove Duplicate Characters from String using set

Supose we have a string,

"Wakanda-Warrior"

We want to delete the duplicate characters from this string and keep the strings in order. The final string should be like,

"Waknd-rio"

There are different ways to do this. Let’s discuss them one by one.

Advertisements

Remove Duplicate Characters from String using set[] and sorted[]

Pass the string to the set[] function. It will return a set of characters containing only unique characters from the given string. Then sort this set by using the str.index[] function as the comparator. It will sort the unique characters in a string based on the index positions of characters in the original string. Then join back the sorted unique characters and assign that to the original string variable. This way, you can remove duplicate characters from the string and keep the order as in the original string.

For Example,

strValue = "Wakanda-Warrior"

# Remove duplicate characters from string and keep the order
strValue = ''.join[sorted[set[strValue], key=strValue.index]]

print[strValue]

Output

Waknd-rio

It deleted all the duplicate characters from the string.

Remove Duplicate Characters from String using OrderedDict

Create an OrderedDict dictionary with characters in a string as keys. It will keep unique characters in the dictionary as keys, and will not change the order of unique characters. Then join back the unique characters [OrderedDict Keys] and assign that to the original string variable. This way, we can remove duplicate characters from the string and will also keep the order as in the original string.

For Example,

from collections import OrderedDict

strValue = "Wakanda-Warrior"

# Remove duplicate characters from string and keep the order
strValue = ''.join[OrderedDict.fromkeys[strValue]] 

print[strValue]

Output

Waknd-rio

It deleted all the duplicate characters from the string.

From Python 3.6 onwards, the dict objects maintain the insertion order by default.

Create a dict object with characters in a string as keys. Then join back the unique characters [dict Keys] and assign that to the original string variable. This way, we can remove duplicate characters from the string and keep the order as in the original string. It will keep only unique characters in the dictionary as keys, and if you are using python 3.6 or later, it will not change the order of unique characters.

For Example,

strValue = "Wakanda-Warrior"

# Remove duplicate characters from string
strValue = ''.join[dict.fromkeys[strValue]] 

print[strValue]

Output

Waknd-rio

It deleted all the duplicate characters from the string.

Remove Duplicate Characters from String using set

After removing the duplicate characters, if keeping the order of unique characters is not a requirement, we can use this technique.

Pass the string to the set[] function. It will return a set of characters containing unique characters from the given string. Then join back these unique characters and assign that to the original string variable. This way, you can remove duplicate characters from the string. But the order of the remaining unique characters will not be the same as in the original string.

For Example,

strValue = "Wakanda-Warrior"

# Remove duplicate characters from string
strValue = ''.join[set[strValue]] 

print[strValue]

Output

iWrnkdoa-

It deleted all the duplicate characters from the string.

Summary

We learned about different ways to delete duplicate characters from a string in Python.

In this post, we will see how to count repeated characters in a string.
Algorithm
Step 1: Declare a String and store it in a variable.
Step 2: Use 2 loops to find the duplicate characters. Outer loop will be used to select a character and initialize variable count to 1.
Step 3: Inner loop will be used to compare the selected character with remaining characters of the string.
Step 4: If a match found, it increases the count by 1.
Step 5: After completion of inner loop, if count of character is greater than 1, then it has duplicates in the string.
Step 6: Print all the repeated characters along with character count
Step 7: End
Example

Input: "welcome to the world of python programming"
Output:
Duplicate characters in a given string: 
w  -  2
e  -  3
l  -  2
o  -  6
m  -  3
t  -  3
h  -  2
r  -  3
p  -  2
n  -  2
g  -  2

Program

string = "welcome to the world of python programming";
   
print["Duplicate characters in a given string: "];  
for i in range[0, len[string]]:  
    count = 1;  
    for j in range[i+1, len[string]]:  
        if[string[i] == string[j] and string[i] != ' ']:  
            count = count + 1;  
            string = string[:j] + '0' + string[j+1:];  
   
    if[count > 1 and string[i] != '0']:  
        print[string[i]," - ",count];

Output

Duplicate characters in a given string: 
w  -  2
e  -  3
l  -  2
o  -  6
m  -  3
t  -  3
h  -  2
r  -  3
p  -  2
n  -  2
g  -  2

How do you find duplicate characters in a string in Python?

Output.
Take a string input using the input[] function..
Declare an empty list called dups..
Iterate through the string using a for loop, using the . count[] function check if the occurrence of a character is more than one in the giver string, if true, append it to the dups list..
Print the dups list..

How do you find duplicate characters in a string?

To find the duplicate character from the string, we count the occurrence of each character in the string. If count is greater than 1, it implies that a character has a duplicate entry in the string. In above example, the characters highlighted in green are duplicate characters.

How do I find a repeated word in a string in Python?

Approach is simple,.
First split given string separated by space..
Now convert list of words into dictionary using collections. Counter[iterator] method. Dictionary contains words as key and it's frequency as value..
Now traverse list of words again and check which first word has frequency greater than 1..

How do you find consecutive repeated characters in a string in Python?

Given a String, extract all the K-length consecutive characters. Input : test_str = 'geekforgeeeksss is bbbest forrr geeks', K = 3 Output : ['eee', 'sss', 'bbb', 'rrr'] Explanation : K length consecutive strings extracted.

Bài mới nhất

Chủ Đề