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
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:
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:
- Merging Two Sorted Arrays in JavaTitle: Merging Two Sorted Arrays in Java Merging two sorted arrays into a single array is a common operation in… Read more: Merging Two Sorted Arrays in Java
- Frequency of Elements in an ArrayTitle: Frequency of Elements in an Array Using Java Counting the frequency of elements in an array is a fundamental… Read more: Frequency of Elements in an Array
- Calculate the sum of diagonals in a matrixTitle: Calculate the sum of diagonals in a matrix In this blog post, we’ll delve into the world of matrices… Read more: Calculate the sum of diagonals in a matrix
- Binary Search in JavaBinary search is a powerful algorithm used to efficiently find a target value in a sorted array. In this blog… Read more: Binary Search in Java
- Removing Duplicate Elements in Array using JavaRemoving Duplicate Elements in Array using Java Arrays are fundamental in Java, but duplicate elements can clutter your data. In… Read more: Removing Duplicate Elements in Array using Java
- Transpose a 2D Array in JavaTitle: Transpose a 2D Array in Java In Java, transposing a 2D array involves converting its rows into columns and… Read more: Transpose a 2D Array in Java