Home >>Java Programs >Java Program to print the duplicate elements of an array
In this program, we will create a java program to print the duplicate elements present in the array.
In this program, the first loop will select an element and the second loop will iteration through the whole array by comparing the selected element with the other elements. If a match is found then print the duplicate element.
Program:
public class Main {
public static void main(String[] args) {
int [] arr = new int [] {1, 4, 2, 7, 8, 8, 3, 7, 3, 6, 2};
System.out.println("Duplicate elements in given array: ");
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(arr[j]);
}
}
}
}