I coded this in attempt to find the duplicates in an array and increment the count each time a duplicate element was found, this program work but if I put an else statement after if statement the compiler prints off the else statement even though the array has duplicate elements…
public class arraysexpmnt {
public static void main(String[] args) {
int[] arr={2,2,2,5,7,8,9,9,8,7};
int count=0;
for(int i=0;i<arr.length;i++){
for(int j=i+1;j<arr.length;j++){
if(arr[j]==arr[i]){
count++;
System.out.println("Duplicate found! Original is " + arr[i] + " and match is " +arr[j]+" and the count of similar elements is "+count);
}
}
}
}
}
the else clause gets executed whenever in the loop the two elements in the array do not match.. which is a quite common thing. put the same tracing println there and you’ll see it.