Duplicate Elements: Find duplicates in an array

This program demonstrates how to find duplicate elements in an array using Java. The algorithm iterates through the array and uses a HashSet to track seen elements. If an element is found in the HashSet, it is identified as a duplicate.

Java Program

import java.util.HashSet;

public class DuplicateElements {
    /**
     * Finds and prints duplicate elements in the given array.
     *
     * @param array The array to check for duplicates.
     */
    public static void findDuplicates(int[] array) {
        HashSet<Integer> seen = new HashSet<>();
        HashSet<Integer> duplicates = new HashSet<>();
        
        for (int element : array) {
            if (!seen.add(element)) {
                duplicates.add(element);
            }
        }
        
        if (duplicates.isEmpty()) {
            System.out.println("No duplicates found.");
        } else {
            System.out.println("Duplicate elements: " + duplicates);
        }
    }

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 2, 3, 8, 9, 10, 10};
        findDuplicates(array);
    }
}

Explanation

The program includes the following components:

  • import java.util.HashSet;: Imports the HashSet class from the Java Collections Framework.
  • public class DuplicateElements: Defines the main class.
  • public static void findDuplicates(int[] array): Defines a static method that takes an array of integers as input and prints the duplicate elements.
  • HashSet<Integer> seen = new HashSet<>();: Initializes a HashSet to keep track of elements that have been encountered in the array.
  • HashSet<Integer> duplicates = new HashSet<>();: Initializes a HashSet to store duplicate elements.
  • for (int element : array): Iterates through each element in the array.
  • if (!seen.add(element)): Checks if the element can be added to the seen set. If it returns false, it means the element is a duplicate.
  • duplicates.add(element): Adds the duplicate element to the duplicates set.
  • if (duplicates.isEmpty()): Checks if the duplicates set is empty and prints an appropriate message.
  • public static void main(String[] args): The main method that initializes an array and calls the findDuplicates method.

The example array int[] array = {1, 2, 3, 4, 5, 6, 7, 2, 3, 8, 9, 10, 10}; contains the duplicates 2, 3, and 10. When the findDuplicates method is called, it will print:

No duplicates found.
Duplicate elements: [2, 3, 10]

 

Explanation

  • Java Program: The provided Java program utilizes the HashSet class to identify duplicate elements in an array. The findDuplicates method iterates through the array, adding each element to a HashSet called seen. If an element cannot be added (because it is already in the seen set), it is added to a HashSet called duplicates.
  • Output: The output of the program will list all the duplicate elements found in the array.

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)