Given 2 String arrays arr1 and arr2 as input, find and return the common elements.
| Input Parameters |
Expected outputs |
strArray1 = {"hello", "good", "morning"};
strArray2 = {"good"};
|
Common Elements : good
|
strArray1 = {"great", "big", "boat"};
strArray2 = {"good", "great", "boat"};
|
Common Elements : great,boat
|
strArray1 = {"north", "south", "east"};
strArray2 = {"west"};
|
No common elements are present
|
| The java method should accept following input parameters: arr1 (String array), arr2 (String array).
|
| Initialize a variable set1 of type HashSet to hold the input elements.
|
| Initialize and ArrayList variable named commonAl to store the common elements that we find in the two input arrays.
|
| 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 set1.
|
| 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:
If arr2[idx] in already present in set1:
If commonAl does not contain arr2[idx]:
Add arr2[idx] to commonAl.
|
| At completion of above loop, commonAl contains all the common elements.
|
| Use an iterator to access elements from commonAl and add the same to String array named commonArr. Return commonArr.
|
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.ArrayList;
import java.util.HashSet;
public class SetArraysFindCommon {
public String[] setArraysFindCommon(String[] arr1, String[] arr2) {
HashSet<String> set1=new HashSet<String>();
ArrayList<String> commonAl=new ArrayList<String>();
for (int idx=0; idx < arr1.length; idx++) {
set1.add(arr1[idx]);
}
for (int idx=0; idx < arr2.length; idx++) {
if(set1.contains(arr2[idx])) {
if(!commonAl.contains(arr2[idx])) {
commonAl.add(arr2[idx]);
}
}
}
String[] commonArr= commonAl.toArray(new String[commonAl.size()]);
return commonArr;
}
public static void main(String[] args) {
SetArraysFindCommon sp=new SetArraysFindCommon();
String[] retVal;
try {
String[] strArray1 = {"hello", "good", "morning"};
String[] strArray2 = {"good"};
printArraySummary(strArray1, "Original Array1");
printArraySummary(strArray2, "Original Array2");
retVal=sp.setArraysFindCommon(strArray1, strArray2);
printArraySummary(retVal, "Common Elements");
String[] strArray3 = {"great", "big", "boat"};
String[] strArray4 = {"good", "great", "boat"};
printArraySummary(strArray3, "Original Array1");
printArraySummary(strArray4, "Original Array2");
retVal=sp.setArraysFindCommon(strArray3, strArray4);
printArraySummary(retVal, "Common Elements");
String[] strArray5 = {"north", "south", "east"};
String[] strArray6 = {"west"};
printArraySummary(strArray5, "Original Array1");
printArraySummary(strArray6, "Original Array2");
retVal=sp.setArraysFindCommon(strArray5, strArray6);
printArraySummary(retVal, "Common Elements");
}catch (Exception exception) {
System.out.print("Exception,"+ exception);
exception.printStackTrace();
}
}
public static void printArraySummary(String[] 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;
}
}