Program to find the duplicate characters in a string

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#




Latest Courses