Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

how do you print duplicate characters from a string?

import java.util.Scanner;
public class uniqueCharacters {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a string:");
        String str = scan.nextLine();

        char currentChar;
        int count=0;
        String duplicateCharacter="";
        for (int i = 0; i < str.length(); i++) {
            currentChar=str.charAt(i);

            for (int j = 0; j < str.length(); j++) {
                if (currentChar==str.charAt(j)) {
                    count++;
                }
            }
            
            if(count!=1 && !duplicateCharacter.contains(currentChar+"")){

                duplicateCharacter+=currentChar+" ";
            }
            count=0;
        }

        System.out.println(duplicateCharacter);
    }
}
 
PREVIOUS NEXT
Tagged: #print #duplicate #characters
ADD COMMENT
Topic
Name
8+9 =