import java.util.Arrays;
public class DuplicateElements {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 3, 5, 6, 2};
int count = 0;
System.out.println("Original Array: " + Arrays.toString(arr));
for(int i=0; i<arr.length; i++){
for(int j=i+1; j<arr.length; j++){
if(arr[i] == arr[j]){
System.out.println("Duplicate element found: " + arr[j]);
count++;
break;
}
}
}
if(count == 0){
System.out.println("No duplicate elements found.");
}
}
}
Original Array: [1, 2, 3, 4, 3, 5, 6, 2]
Duplicate element found: 2
Duplicate element found: 3
arr
. We first print the original array using Arrays.toString()
method. Then, we have two nested loops.