Thursday, February 24, 2022

How to Check if an Array Contain Duplicate In Java?

Hello All, 

Here the coding snippet to check duplicate in array. I used char[] to check the duplicate character check in a string. 


private static void findDuplicate(String input){
char[] strChar = input.toCharArray();
boolean haveDuplicate = false;
for(int i = 0 ; i < input.length(); i++){
for(int j = i+1 ; j < input.length(); j++){
if(strChar[i] == strChar[j]){
haveDuplicate = true;
System.out.print(strChar[i]);
}
}
}
if(!haveDuplicate){
System.out.println("No duplicate found in array");
}
}
Let pass input as "Java".
Output : a

Pass input as "Python"
Output : No duplicate found in array

No comments:

Post a Comment