Question
Find the duplicate characters of given word
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class duplicateCharactersInWord
{
public static void main(String[] args)
{
int i=0,j=0,count=0;
String wd="";
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word");
wd =sc.next();
/*Converts given word into character array*/
char characters[] = wd.toCharArray();
System.out.println("Duplicate characters in "+wd+":");
for(i = 0; i < characters.length; i++)
{
count = 0;
for(j = i+1; j < characters.length; j++)
{
if(characters[i] == characters[j])
{
count++;
/*Set characters[j] to ~ to avoid printing visited character*/
characters[j] = '~';
}
}
/*A character is considered as duplicate if count is greater than 1 */
if(count > 0 && characters[i] != '~')
{
System.out.println(characters[i]);
}
}
}
}