Signup/Sign In

Java Program to Find the Duplicate Characters in a String

In this tutorial, we will learn how to find duplicate characters in the string. But before moving further, if you are not familiar with the concept of string, then do check the article on Strings in Java.

Input: Enter the string: maximum occurrence

Output: Duplicate Characters in the String are:

m

u

c

r

e

Program 1: Find Duplicate Characters in a String

In this program, we will see how to find the duplicate characters in the string when the string is pre-defined.

Algorithm

  1. Start
  2. Declare a string
  3. Initialize it
  4. Declare a variable to count the frequency of characters.
  5. Convert the string to a character array.
  6. Use two for loops to calculate the frequency of each element in the string.
  7. Use the first for loop to hold the characters of the string.
  8. Initialize the count to 1.
  9. Use the inner for loop to iterate over the rest of the characters in the string.
  10. Check if the character occurs again in the string.
  11. If it occurs, then increment the count.
  12. Set the str[j] to 0 to avoid calculating visited characters.
  13. Check if the value of count is greater than 1 and str[i] is not equal to 0.
  14. If it satisfies the above condition, then print the element.
  15. Stop.

Below is the code for the same in Java language.

//Java Program to find the duplicate characters in a given string
public class Main
{  
     public static void main(String[] args) 
     {  
        String str1 = "Maximum and Minimum";  
        int count;  
        System.out.println("The entered string is: "+str1);
        //Converts given string into character array  
        char str[] = str1.toCharArray();  
          
        System.out.println("Duplicate characters in a given string: ");  
        //Count the frequency of each character present in the string  
        for(int i = 0; i <str.length; i++) 
        {  
            count = 1;  
            for(int j = i+1; j <str.length; j++) 
            {  
                if(str[i] == str[j] && str[i] != ' ') 
                {  
                    count++;  
                    //Set string[j] to 0 to avoid printing visited character  
                    str[j] = '0';  
                }  
            }  
            //A character is considered as duplicate if count is greater than 1  
            if(count > 1 && str[i] != '0')  
                System.out.println(str[i]);  
        }  
    }  
}  


The entered string is: Maximum and Minimum
Duplicate characters in a given string:
M
a
i
m
u
n

Program 2: Find the Duplicate Characters in a String

In this program, we will see how to find the duplicate characters in the string when the string is user-defined. Here, first, we will ask the user to enter the string and then will check for duplicate elements.

Algorithm

  1. Start
  2. Declare a string
  3. Ask the user to initialize it
  4. Declare a variable to count the frequency of characters.
  5. Convert the string to a character array.
  6. Use two for loops to calculate the frequency of each element in the string.
  7. Use the first for loop to hold the characters of the string.
  8. Initialize the count to 1.
  9. Use the inner for loop to iterate over the rest of the characters in the string.
  10. Check if the character occurs again in the string.
  11. If it occurs, then increment the count.
  12. Set the str[j] to 0 to avoid calculating visited characters.
  13. Check if the value of count is greater than 1 and str[i] is not equal to 0.
  14. If it satisfies the above condition, then print the element.
  15. Stop.

Below is the code for the same in Java language.

//Java Program to find the duplicate characters in a given string
import java.util.*;
public class Main
{  
     public static void main(String[] args) 
     {  
         //Take input from the user
        Scanner sc=new Scanner(System.in);  
        System.out.println("Enter the string is: ");
        String str1=sc.nextLine();
        int count;  
        //Converts given string into character array  
        char str[] = str1.toCharArray();            
        System.out.println("Duplicate characters in the given string: ");  
        //Counts each character present in the string  
        for(int i = 0; i <str.length; i++) 
        {  
            count = 1;  
            for(int j = i+1; j <str.length; j++) 
            {  
                if(str[i] == str[j] && str[i] != ' ') 
                {  
                    count++;  
                    //Set string[j] to 0 to avoid printing visited character  
                    str[j] = '0';  
                }  
            }  
            //A character is considered as duplicate if count is greater than 1  
            if(count > 1 && str[i] != '0')  
                System.out.println(str[i]);  
        }  
    }  
}  


Enter the string is: Duplicate Characters
Duplicate characters in the given string:
c
a
t
e
r



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.