Java program to find the duplicate characters in a string1 min read

We write a program to find duplicate characters in a string in java. For example, consider a string “nut cutter“, duplicate characters in it are : u: 2, t : 3.

First, we created a string variable and take the user input for a string to compare with. And initialize a variable num to keep the number of counts. Then we convert the string variable to characters by creating a character array. Then with iteration(for loop), we compare the two characters and if the character of the compared index is matched, then we display that character with the increase of num with each iteration.
The for loop will continue till the length of the entered string is finished.

Java program of string to check duplicate characters.

The output of duplicate characters in java

Also, learn
How to remove duplicates from ArrayList in Java