Home >>Java Programs >Java program to find the duplicate characters in a string
In this example, we will create a java program to find the duplicate characters in the string.
To find the duplicate character from the string we will count the occurrence of each character in the string. If the count is greater than 1 then it implies that a character has a duplicate entry in the string.
public class Main
{
public static void main(String[] args)
{
String string1 = "ABCD ASDF";
int count;
char string[] = string1.toCharArray();
System.out.println("Duplicate characters in a given string: ");
for(int i = 0; i <string.length; i++)
{
count = 1;
for(int j = i+1; j <string.length; j++)
{
if(string[i] == string[j] && string[i] != ' ')
{
count++;
string[j] = '0';
}
}
if(count > 1 && string[i] != '0')
System.out.println(string[i]);
}
}
}