Given two input arrays of the same data type, merge the 2 arrays and remove the duplicates from the merged array.
| Input Parameters |
Expected outputs |
intArray1 = {1,2,3,4};
intArray2 = {5,2,7,4};
|
1,2,3,4,5,7
|
intArray3 = {7,8};
intArray4 = {7,8};
|
7,8
|
| The java method should accept following input parameters: arr1 (int array), arr2 (int array).
|
| Initialize a variable merged of type LinkedHashSet to hold the input elements.
|
| Using a for loop iterate through arr1, using idx as a loop variable with initial value 0 and increment idx till it reaches arr1.length-1:
Add arr1[idx] to merged.
|
| Using a for loop iterate through arr2, using idx as a loop variable with initial value 0 and increment idx till it reaches arr2.length-1:
Add arr2[idx] to merged.
|
| At completion of above loop, merged contains all the unique elements from arr1 and arr2 with duplicates removed.
|
| Use an iterator to access elements from merged and add the same to integer array named retVal. Return retVal.
|
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
import java.util.HashSet;
import java.util.Iterator;
public class SetMergeArrays {
public int[] setMergeArrays(int[] arr1, int[] arr2) {
HashSet<Integer> merged=new HashSet<Integer>();
for (int idx=0; idx <arr1.length; idx++) {
merged.add(arr1[idx]);
}
for (int idx=0; idx <arr2.length; idx++) {
merged.add(arr2[idx]);
}
int[] retVal=new int[merged.size()];
int idx=0;
Iterator<Integer> itr=merged.iterator();
while (itr.hasNext()) {
retVal[idx]=itr.next().intValue();
idx++;
}
return retVal;
}
public static void main(String[] args) {
SetMergeArrays sp=new SetMergeArrays();
int[] retVal;
try {
int[] intArray1 = {1,2,3,4};
int[] intArray2 = {5,2,7,4};
printArraySummary(intArray1, "Original Array1");
printArraySummary(intArray2, "Original Array2");
retVal=sp.setMergeArrays(intArray1, intArray2);
printArraySummary(retVal, "Merged Arrays");
int[] intArray3 = {7,8};
int[] intArray4 = {7,8};
printArraySummary(intArray3, "Original Array1");
printArraySummary(intArray4, "Original Array2");
retVal=sp.setMergeArrays(intArray3, intArray4);
printArraySummary(retVal, "Merged Arrays");
int[] intArray5 = {1,2};
int[] intArray6 = {3,4};
printArraySummary(intArray5, "Original Array1");
printArraySummary(intArray6, "Original Array2");
retVal=sp.setMergeArrays(intArray5, intArray6);
printArraySummary(retVal, "Merged Arrays");
}catch (Exception exception) {
System.out.print("Exception,"+ exception);
exception.printStackTrace();
}
}
public static void printArraySummary(int[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("************************************************************************");
System.out.print(label+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex< intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex< intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}