I am trying to print duplicate elements in one d array using for each loop. But my output was an unexpected one. Could anyone please assist?
package Login;
public class DupsArray {
static int[] a = {1,2,3,3};
public static void main(String[] args) {
int length = a.length;
for(int i=0;i<=length-1;i++) {
for(int j : a) {
for(j=1;j<=length-1;j++) {
if(a[i]==(a[j]) ) {
System.out.println("Found duplicate");
} else {
System.out.println("No duplicates found");
}
}
}
}
}
The results show as follows:
The expected results to be print duplicate found.