diff --git a/core-java-modules/core-java-arrays-guides/src/main/java/com/baeldung/mergeandremoveduplicate/MergeArraysAndRemoveDuplicate.java b/core-java-modules/core-java-arrays-guides/src/main/java/com/baeldung/mergeandremoveduplicate/MergeArraysAndRemoveDuplicate.java new file mode 100644 index 0000000000..e0b249565f --- /dev/null +++ b/core-java-modules/core-java-arrays-guides/src/main/java/com/baeldung/mergeandremoveduplicate/MergeArraysAndRemoveDuplicate.java @@ -0,0 +1,96 @@ +package com.baeldung.mergeandremoveduplicate; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Stream; + +public class MergeArraysAndRemoveDuplicate { + + public static int[] removeDuplicateOnSortedArray(int[] arr) { + // Initialize a new array to store unique elements + int[] uniqueArray = new int[arr.length]; + uniqueArray[0] = arr[0]; + int uniqueCount = 1; + + // Iterate through the sorted array to remove duplicates + for (int i = 1; i < arr.length; i++) { + if (arr[i] != arr[i - 1]) { + uniqueArray[uniqueCount] = arr[i]; + uniqueCount++; + } + } + int[] truncatedArray = new int[uniqueCount]; + System.arraycopy(uniqueArray, 0, truncatedArray, 0, uniqueCount); + return truncatedArray; + } + + public static int[] mergeAndRemoveDuplicatesUsingStream(int[] arr1, int[] arr2) { + Stream s1 = Arrays.stream(arr1).boxed(); + Stream s2 = Arrays.stream(arr2).boxed(); + return Stream.concat(s1, s2) + .distinct() + .mapToInt(Integer::intValue) + .toArray(); + } + + public static int[] mergeAndRemoveDuplicatesUsingSet(int[] arr1, int[] arr2) { + int[] mergedArr = mergeArrays(arr1, arr2); + Set uniqueInts = new HashSet<>(); + + for (int el : mergedArr) { + uniqueInts.add(el); + } + + return getArrayFromSet(uniqueInts); + } + + private static int[] getArrayFromSet(Set set) { + int[] mergedArrWithoutDuplicated = new int[set.size()]; + int i = 0; + for (Integer el: set) { + mergedArrWithoutDuplicated[i] = el; + i++; + } + return mergedArrWithoutDuplicated; + } + + public static int[] mergeAndRemoveDuplicates(int[] arr1, int[] arr2) { + return removeDuplicate(mergeArrays(arr1, arr2)); + } + + public static int[] mergeAndRemoveDuplicatesOnSortedArray(int[] arr1, int[] arr2) { + return removeDuplicateOnSortedArray(mergeArrays(arr1, arr2)); + } + + private static int[] mergeArrays(int[] arr1, int[] arr2) { + int[] mergedArrays = new int[arr1.length + arr2.length]; + System.arraycopy(arr1, 0, mergedArrays, 0, arr1.length); + System.arraycopy(arr2, 0, mergedArrays, arr1.length, arr2.length); + + return mergedArrays; + } + private static int[] removeDuplicate(int[] arr) { + int[] withoutDuplicates = new int[arr.length]; + int i = 0; + + for(int element : arr) { + if(!isElementPresent(withoutDuplicates, element)) { + withoutDuplicates[i] = element; + i++; + } + } + int[] truncatedArray = new int[i]; + System.arraycopy(withoutDuplicates, 0, truncatedArray, 0, i); + return truncatedArray; + } + + private static boolean isElementPresent(int[] arr, int element) { + for(int el : arr) { + if(el == element) { + return true; + } + } + return false; + } +} diff --git a/core-java-modules/core-java-arrays-guides/src/test/java/com/baeldung/mergeandremoveduplicate/MergeArraysAndRemoveDuplicateUnitTest.java b/core-java-modules/core-java-arrays-guides/src/test/java/com/baeldung/mergeandremoveduplicate/MergeArraysAndRemoveDuplicateUnitTest.java new file mode 100644 index 0000000000..ce8d69b445 --- /dev/null +++ b/core-java-modules/core-java-arrays-guides/src/test/java/com/baeldung/mergeandremoveduplicate/MergeArraysAndRemoveDuplicateUnitTest.java @@ -0,0 +1,74 @@ +package com.baeldung.mergeandremoveduplicate; + +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +public class MergeArraysAndRemoveDuplicateUnitTest { + + private final Logger logger = LoggerFactory.getLogger(MergeArraysAndRemoveDuplicateUnitTest.class); + + private static String getCommaDelimited(int[] arr) { + return Arrays.stream(arr) + .mapToObj(Integer::toString) + .collect(Collectors.joining(", ")); + } + + @Test + public void givenNoLibrary_whenArr1andArr2_thenMergeAndRemoveDuplicates() { + int[] arr1 = {3, 2, 1, 4, 5, 6, 8, 7, 9}; + int[] arr2 = {8, 9, 10, 11, 12, 13, 15, 14, 15, 14, 16, 17}; + int[] expectedArr = {3, 2, 1, 4, 5, 6, 8, 7, 9, 10, 11, 12, 13, 15, 14, 16, 17}; + + int[] mergedArr = MergeArraysAndRemoveDuplicate.mergeAndRemoveDuplicates(arr1, arr2); + + //merged array maintains the order of the elements in the arrays + assertArrayEquals(expectedArr, mergedArr); + + logger.info(getCommaDelimited(mergedArr)); + } + + @Test + public void givenNoLibrary_whenArr1andArr2_thenMergeSortAndRemoveDuplicates() { + int[] arr1 = {1, 2, 3, 4, 5, 5, 6, 7, 7, 8}; + int[] arr2 = {8, 9, 10, 11, 12, 13, 14, 15, 15, 16, 17}; + int[] expectedArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}; + + int[] mergedArr = MergeArraysAndRemoveDuplicate.mergeAndRemoveDuplicatesOnSortedArray(arr1, arr2); + + assertArrayEquals(expectedArr, mergedArr); + + logger.info(getCommaDelimited(mergedArr)); + } + + @Test + public void givenSet_whenArr1andArr2_thenMergeAndRemoveDuplicates() { + int[] arr1 = {3, 2, 1, 4, 5, 6, 8, 7, 9}; + int[] arr2 = {8, 9, 10, 11, 12, 13, 15, 14, 15, 14, 16, 17}; + int[] expectedArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}; + + int[] mergedArr = MergeArraysAndRemoveDuplicate.mergeAndRemoveDuplicatesUsingSet(arr1, arr2); + + assertArrayEquals(expectedArr, mergedArr); + + logger.info(getCommaDelimited(mergedArr)); + } + + @Test + public void givenStream_whenArr1andArr2_thenMergeAndRemoveDuplicates() { + int[] arr1 = {3, 2, 1, 4, 5, 6, 8, 7, 9}; + int[] arr2 = {8, 9, 10, 11, 12, 13, 15, 14, 15, 14, 16, 17}; + int[] expectedArr = {3, 2, 1, 4, 5, 6, 8, 7, 9, 10, 11, 12, 13, 15, 14, 16, 17}; + + int[] mergedArr = MergeArraysAndRemoveDuplicate.mergeAndRemoveDuplicatesUsingStream(arr1, arr2); + + assertArrayEquals(expectedArr, mergedArr); + + logger.info(getCommaDelimited(mergedArr)); + } +}