How do i find a repeated character in a string in python?

It’s easy in Python to find the repeated character in a given string. There are many ways to do it like using alphabets, for-loop, or collections.

Simple example code.

Basic ways scan the string 26 times

chars = "abcdefghijklmnopqrstuvwxyz"
check_string = "Write a Python program to find the repeated character in a given string"
l_string = check_string.lower()

for char in chars:
    count = l_string.count(char)
    print(char, count, end=', ')

Output:

How do i find a repeated character in a string in python?

Another way is only to go through the string once

Space will also count in this method so apply if condition to remove space in the count.


check_string = "Write a Python program to find the repeated character in a given string"
count = {}
for s in check_string:
    if s != ' ':
        if s in count:
            count[s] += 1
        else:
            count[s] = 1
print(count)

Using collections

Need to import collection module.

import collections

check_string = "Write a Python program to find the repeated character in a given string"
d = collections.defaultdict(int)
for c in check_string:
    d[c] += 1

print(d)

Do comment if you have any doubts and suggestions on this Python char program.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

How do i find a repeated character in a string in python?

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

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

Explanation

In this program, we need to find the duplicate characters in the 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.

Algorithm

  1. Define a string.
  2. Two loops will be used to find the duplicate characters. Outer loop will be used to select a character and initialize variable count by 1.
  3. Inner loop will compare the selected character with rest of the characters present in the string.
  4. If a match found, it increases the count by 1 and set the duplicates of selected character by '0' to mark them as visited.
  5. After inner loop, if count of character is greater than 1, then it has duplicates in the string.

Solution

Python

Output:

 Duplicate characters in a given string: 
r
e
t
s
i

C

Output:

Duplicate characters in a given string: 
r
e
t
s
i

JAVA

Output:

Duplicate characters in a given string: 
r
e
t
s
i

C#

Output:

Duplicate characters in a given string: 
r
e
t
s
i

PHP

Output:

Duplicate characters in a given string: 
r
e
t
s
i

Next Topic#

How do I find a repeated character in a string?

Given a string, find the first repeated character in it..
Copy the given array to an auxiliary array temp[]..
Sort the temp array using a O(N log N) time sorting algorithm..
Scan the input array from left to right. For every element, count its occurrences in temp[] using binary search..

How do you get a repeated value in Python?

Multiple Ways To Check if duplicates exist in a Python list.
Length of List & length of Set are different..
Check each element in set. if yes, dup, if not, append..
Check for list.count() for each element..