Delete comment from: Java67
If you want only to print duplicate characters...
Here, I'm iterating through only half of the String. Suggestions for improvement are always welcome :)
// Print duplicate characters of a String.
public static void printDuplicates(String str) {
if (str.length() == 1) {
System.out.println("No duplicate found!");
return;
}
char[] arr = str.toCharArray();
Set setOfDuplicates=new HashSet();
int length = arr.length;
List list=new ArrayList();
for (int i = 0; i < (length) / 2; i++) {
System.out.println("i= "+i);
if(list.contains(arr[i]))
setOfDuplicates.add(arr[i]);
else
list.add(arr[i]);
if(list.contains(arr[length-i-1]))
setOfDuplicates.add(arr[length-i-1]);
else
list.add(arr[length-i-1]);
}
System.out.println(setOfDuplicates);
}
May 23, 2018, 3:11:35 PM
Posted to How to Find Duplicate Characters in String [Java Coding Problems]