Merge Two Arrays and Delete Duplicates in Java
In Java programming, working with arrays is a common task. Often, we may find ourselves needing to merge two arrays while ensuring that there are no duplicate elements in the resulting array. This process involves careful manipulation of array elements and efficient algorithms to achieve the desired outcome. This article will explore how to merge two arrays and remove duplicates in Java, providing code examples.
1. Overview
Consider a scenario where we have two arrays representing the scores of students in two different exams. We need to combine these arrays to get a comprehensive list of scores, ensuring that no duplicate scores are included:
int[] exam1Scores = {85, 90, 75, 80}; int[] exam2Scores = {90, 85, 95, 80}; // Merge the arrays and remove duplicates int[] combinedScores = mergeAndRemoveDuplicates(exam1Scores, exam2Scores); System.out.println("Combined Scores: " + Arrays.toString(combinedScores));
Output should be:
Combined Scores: [75, 80, 85, 90, 95]
2. Merging and Removing Duplicates Using Set
2.1 Step 1: Merge Two Arrays
First, let’s explore how to merge two arrays in Java. We can achieve this by creating a new array with a size equal to the sum of the lengths of the two arrays, and then copying elements from each array into the new array. Here’s how we can implement it:
public class ArrayMerger { public static int[] mergeArrays(int[] arr1, int[] arr2) { int len1 = arr1.length; int len2 = arr2.length; int[] mergedArray = new int[len1 + len2]; System.arraycopy(arr1, 0, mergedArray, 0, len1); System.arraycopy(arr2, 0, mergedArray, len1, len2); return mergedArray; } public static void main(String[] args) { int[] array1 = {1, 2, 3, 6}; int[] array2 = {3, 4, 5, 1}; int[] mergedArray = mergeArrays(array1, array2); System.out.println("Merged Array: " + Arrays.toString(mergedArray)); } }
In the above code, we use System.arraycopy()
to copy elements from both arrays into the merged array. Finally, we return the merged array.
2.2 Step 2: Removing Duplicates
After merging the arrays, we need to remove any duplicate elements from the resulting array. We can achieve this by iterating through the array and using a HashSet
to store unique elements. Here’s how we can implement it:
public class ArrayDuplicateRemover { public static int[] removeDuplicates(int[] arr) { Set<Integer> set = new HashSet<>(); List<Integer> resultList = new ArrayList<>(); for (int value : arr) { if (!set.contains(value)) { resultList.add(value); set.add(value); } } // Convert ArrayList to int[] int[] resultArray = new int[resultList.size()]; for (int i = 0; i < resultList.size(); i++) { resultArray[i] = resultList.get(i); } return resultArray; } public static void main(String[] args) { int[] mergedArray = {1, 2, 3, 3, 4, 5, 6, 7, 7}; int[] uniqueArray = removeDuplicates(mergedArray); System.out.println("Array with Duplicates Removed: " + Arrays.toString(uniqueArray)); } }
In this code, we use a HashSet
to store unique elements while iterating through the array. The removeDuplicates
method efficiently removes duplicates from the array while preserving the order of elements. This is achieved by using a HashSet
to keep track of unique elements encountered. If an element is encountered for the first time, it’s added to both the resultList
and the HashSet
. If it’s encountered again, it’s ignored. Finally, the resultList
is converted back to an array.
2.3 Sort the Resultant Array
Sorting the resultant array after merging and removing duplicates can further enhance its usability and readability. Here’s the example with sorting included:
Arrays.sort(uniqueArray);
In this code, after merging the arrays and removing duplicates, we apply Arrays.sort()
to the uniqueArray
, ensuring that the final result is sorted in ascending order.
2.4 Combining Merging and Removing Duplicates
Now, let’s combine both merging and removing duplicates into a single program:
public class MergeAndRemoveDuplicates { public static int[] mergeAndRemoveDuplicates(int[] arr1, int[] arr2) { int[] mergedArray = ArrayMerger.mergeArrays(arr1, arr2); int[] uniqueArray = ArrayDuplicateRemover.removeDuplicates(mergedArray); Arrays.sort(uniqueArray); return uniqueArray; } public static void main(String[] args) { int[] array1 = {1, 2, 3, 4, 5}; int[] array2 = {2, 3, 4, 5, 6}; int[] result = mergeAndRemoveDuplicates(array1, array2); System.out.println("Merged Array with Duplicates Removed: " + Arrays.toString(result)); } }
This program first merges the two arrays using ArrayMerger.mergeArrays()
and then removes duplicates using ArrayDuplicateRemover.removeDuplicates()
.
The output of running the program is:
3. Merging and Removing Duplicates Using Stream
3.1 Merging Two Arrays Using Stream
With Java streams, we can accomplish the merging of two arrays more succinctly and elegantly. We can utilize the Stream.concat()
method to concatenate the arrays into a single stream:
public class ArrayMergerStreams { public static int[] mergeArrays(int[] arr1, int[] arr2) { return IntStream.concat(Arrays.stream(arr1), Arrays.stream(arr2)) .toArray(); } public static void main(String[] args) { int[] array1 = {1, 2, 3}; int[] array2 = {3, 4, 5}; int[] mergedArray = mergeArrays(array1, array2); System.out.println("Merged Array: " + Arrays.toString(mergedArray)); } }
In this code, IntStream.concat()
concatenates the streams generated from the arrays, and .toArray()
converts the resulting stream back into an array.
3.2 Removing Duplicates Using Stream
Java streams offer a clean way to remove duplicate elements from an array. We can use the distinct()
method to filter out duplicate elements like this:
public class ArrayDuplicateRemoverStreams { public static int[] removeDuplicates(int[] arr) { return Arrays.stream(arr) .distinct() .toArray(); } public static void main(String[] args) { int[] mergedArray = {1, 2, 3, 3, 4, 5}; int[] uniqueArray = removeDuplicates(mergedArray); System.out.println("Array with Duplicates Removed: " + Arrays.toString(uniqueArray)); } }
In this code, Arrays.stream(arr)
creates a stream from the array, .distinct()
filters out duplicate elements, and .toArray()
converts the stream back into an array.
3.3 Combining Merging and Removing Duplicates Using Streams
Now, let’s combine both merging and removing duplicates using streams into a single program:
public class MergeAndRemoveDuplicatesUsingStream { public static int[] mergeAndRemoveDuplicates(int[] arr1, int[] arr2) { return Arrays.stream(mergeArrays(arr1, arr2)) .distinct() .toArray(); } public static int[] mergeArrays(int[] arr1, int[] arr2) { return IntStream.concat(Arrays.stream(arr1), Arrays.stream(arr2)) .toArray(); } public static void main(String[] args) { int[] array1 = {1, 2, 3}; int[] array2 = {3, 4, 5}; int[] result = mergeAndRemoveDuplicates(array1, array2); System.out.println("Merged Array with Duplicates Removed: " + Arrays.toString(result)); } }
Here’s a breakdown of the code:
mergeAndRemoveDuplicates
method:- This method takes two integer arrays
arr1
andarr2
as input. - It first calls the
mergeArrays
method to concatenate the arraysarr1
andarr2
. - Then it converts the concatenated array into a stream using
Arrays.stream
. - The
distinct
method is called on the stream to eliminate duplicate elements. - Finally, the
toArray
method is used to convert the stream back into an integer array, which is then returned.
- This method takes two integer arrays
mergeArrays
method:- This method takes two integer arrays
arr1
andarr2
as input. - It concatenates the arrays
arr1
andarr2
into a single stream of integers usingIntStream.concat
. - The
toArray
method is called to convert the concatenated stream into an integer array, which is then returned.
- This method takes two integer arrays
main
method:- The
mergeAndRemoveDuplicates
method is called witharray1
andarray2
as arguments, and the result is stored in theresult
array.
- The
The output of running this code would be:
Merged Array with Duplicates Removed: [1, 2, 3, 4, 5, 6]
Note: Both approaches have different characteristics:
- Set-based approach:
- Time complexity: O(n) where n is the total number of elements in both arrays.
- Doesn’t preserve the original order of elements.
- Stream-based approach:
- Time complexity: O(n) due to
distinct()
method. - Preserves the original order of elements encountered in the combined stream.
- Time complexity: O(n) due to
Choose the approach that best suits your specific requirements based on performance considerations and the need to maintain the original order.
4. Conclusion
In this article, we explored different methods for combining two arrays and subsequently eliminating any duplicate elements within them. By following the examples provided in this article, we can efficiently merge two arrays and ensure that the resulting array contains only unique elements.
Furthermore, utilizing Java streams simplifies the process of merging arrays and removing duplicates, offering a more concise and readable solution. By incorporating stream operations, we enhance code clarity and maintainability, making array manipulation tasks more efficient in Java programming.
5. Download the Source Code
This was an example of using Java to merge two arrays and delete duplicates.
You can download the full source code of this example here: java merge two arrays delete duplicates