1. Introduction

Finding duplicate elements in an array is a common problem in Java and other programming languages. This task is essential in various applications, such as database management, error checking, and more. In this tutorial, we will write a Java program to find and print the duplicate elements of an array.

2. Program Steps

1. Initialize an array with some elements.

2. Use nested loops to compare each element in the array with the rest.

3. If any duplicate element is found, print it.

3. Code Program

public class FindDuplicates {

    public static void main(String[] args) {
        // Step 1: Initialize an array with some elements
        int[] array = {1, 2, 3, 4, 2, 7, 8, 8, 3};

        System.out.println("Duplicate elements in given array: ");

        // Step 2: Use nested loops to compare each element in the array with the rest
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                // Step 3: If any duplicate element is found, print it
                if (array[i] == array[j]) {
                    System.out.println(array[j]);
                }
            }
        }
    }
}

Output:

Duplicate elements in given array:
2
3
8

4. Step By Step Explanation

In Step 1, we initialize an array with some integer elements.

During Step 2, we use nested loops to traverse the array. The outer loop fixes one element at a time, and the inner loop compares this element with the rest of the elements in the array.

If a duplicate element is found in Step 3, it is printed to the console. The duplicate elements in the given array are 2, 3, and 8.