Java Program to Remove Duplicate Elements in an Array

Java Program to Remove Duplicate Elements in an Array
Reading Time: 7 minutes

Introduction:

The provided Java program aims to remove duplicate elements from an array using a HashSet. Duplicate elements can be problematic in various scenarios, and having a clean array without duplicates is often necessary. The HashSet data structure is employed to efficiently store unique elements while iterating through the original array.

Algorithm:

  1. Initialization:
    • Create an array named array with the original elements.
    • Print the original array for reference.
  2. Remove Duplicates:
    • Create a HashSet named uniqueSet to store unique elements.
    • Iterate through the original array using an enhanced for loop.
    • Add each element to the HashSet (uniqueSet). The HashSet automatically discards duplicate entries as it only stores unique values.
  3. Convert HashSet to Array:
    • Create a new array named result with a size equal to the number of unique elements in the HashSet (size()).
    • Iterate through the HashSet and populate the result array with unique elements.
  4. Print Result:
    • Print the array without duplicates (result).

Explanation:

  • The program starts by initializing an array with some sample elements.
  • A HashSet (uniqueSet) is used to store unique elements. This is achieved by iterating through the original array and adding each element to the HashSet. Since a HashSet does not allow duplicate entries, it automatically ensures that only distinct elements are retained.
  • After processing the array, a new array (result) is created with a size equal to the number of unique elements in the HashSet.
  • The unique elements are then extracted from the HashSet and stored in the result array.
  • Finally, the program prints both the original array and the array without duplicates.

This approach ensures efficiency and simplicity in removing duplicates from an array in Java.

Code :

				
					import java.util.Arrays;
import java.util.HashSet;
public class RemoveDuplicates {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 2, 5, 6, 3};
        System.out.println("Original Array: " + Arrays.toString(array));
        int[] result = removeDuplicates(array);
        System.out.println("Array without duplicates: " + Arrays.toString(result));
    }
    private static int[] removeDuplicates(int[] array) {
        // Use a HashSet to store unique elements
        HashSet<Integer> uniqueSet = new HashSet<>();
         for (int element : array) {
            uniqueSet.add(element);
        }
        // Convert HashSet back to array
        int[] result = new int[uniqueSet.size()];
        int index = 0;
        for (int element : uniqueSet) {
            result[index++] = element;
        }

        return result;
    }
}

				
			

Output :

				
					Original Array: [1, 2, 3, 4, 2, 5, 6, 3]
Array without duplicates: [1, 2, 3, 4, 5, 6]
				
			

This program uses a HashSet to keep track of unique elements in the array and then converts it back to an array without duplicates.

FAQs :

  1. How can I write a Java program to remove duplicate elements in an array?
  • To remove duplicate elements in a Java array, you can use a HashSet to efficiently store unique elements while iterating through the array. Check out the provided Java program for a practical example.
  1. What is the significance of using a HashSet in a Java program for duplicate numbers in an array?
  • Using a HashSet is crucial in identifying and handling duplicate numbers in a Java array. It automatically ensures that only unique elements are retained, simplifying the process of finding and removing duplicates.
  1. Can you explain the key steps in a Java program to remove duplicates from an array?
  • Certainly! The program involves initializing a HashSet to store unique elements, iterating through the array, adding elements to the HashSet, and then converting the HashSet back to an array. This ensures a clean array without duplicate elements.
  1. Are there alternative approaches to writing a Java program for removing duplicate numbers in an array?
  • While HashSet is a common and efficient choice, you can also explore other methods, such as sorting the array and then identifying and removing duplicates. However, the HashSet approach in the provided program is often more straightforward.
  1. How does the Java program ensure the final array is free of duplicate elements?
  • The program guarantees a duplicate-free array by leveraging the unique property of HashSet. As elements are added to the HashSet, duplicates are automatically discarded. The resulting HashSet is then converted back to an array, providing a clean array without any duplicate numbers.