Removing Duplicate Elements in Array using Java

46
Removing Duplicate Elements in Array using Java
Advertisement

Removing Duplicate Elements in Array using Java

Arrays are fundamental in Java, but duplicate elements can clutter your data. In this guide, we’ll explore a simple yet effective way of Removing Duplicate Elements in Array. The program will not only clean up your data but also showcase Java’s versatility in array manipulation.

Java Program: Removing Duplicate Elements

Java
import java.util.Arrays;
import java.util.HashSet;

public class RemoveDuplicatesFromArray {
    // Method: Remove duplicate elements from an array
    public static int[] removeDuplicates(int[] array) {
        // Use HashSet to efficiently remove duplicates while preserving order
        HashSet<Integer> uniqueSet = new HashSet<>();

        // Iterate through the array, adding elements to the set
        for (int value : array) {
            uniqueSet.add(value);
        }

        // Convert the HashSet back to an array
        int[] result = new int[uniqueSet.size()];
        int index = 0;
        for (int uniqueValue : uniqueSet) {
            result[index++] = uniqueValue;
        }

        return result;
    }

    // Main method: Test the removal of duplicate elements
    public static void main(String[] args) {
        // Example array with duplicate elements
        int[] arrayWithDuplicates = {5, 2, 7, 8, 2, 5, 3, 7};

        // Call the removeDuplicates method
        int[] arrayWithoutDuplicates = removeDuplicates(arrayWithDuplicates);

        // Print the original and modified arrays
        System.out.println("Original Array with Duplicates: " + Arrays.toString(arrayWithDuplicates));
        System.out.println("Array After Removing Duplicates: " + Arrays.toString(arrayWithoutDuplicates));
    }
}

Breaking Down the Implementation:

Method removeDuplicates:

  • Declares a HashSet named uniqueSet to efficiently store unique elements while preserving their order.
  • Iterates through the input array using an enhanced for loop.
  • Adds each element to the uniqueSet, automatically removing duplicates.

Main Method:

  • Declares an example array named arrayWithDuplicates containing duplicate elements.
  • Calls the removeDuplicates method, passing the array with duplicates.
  • The method returns an array without duplicates, which is stored in the variable arrayWithoutDuplicates.
  • Prints both the original array with duplicates and the modified array without duplicates using Arrays.toString().

Output:

Java
Original Array with Duplicates: [5, 2, 7, 8, 2, 5, 3, 7]
Array After Removing Duplicates: [2, 3, 5, 7, 8]

Please follow Arrays programs to learn more: Array

Conclusion:

This Java program provides a straightforward yet powerful solution for removing duplicate elements from an array. Leveraging HashSet not only simplifies the process but also ensures efficient removal of duplicates. This approach showcases Java’s elegance in addressing common array manipulation challenges. Incorporate this solution into your Java arsenal for cleaner and more streamlined arrays. Happy coding!

Latest Posts: