| Input Parameters |
Expected outputs |
strArray1 = {"hi", "hello", "hola"};
strArray2 = {"good", "better", "best"};
|
The arrays are disjoint!
|
strArray3 = {"great", "big", "ship"};
strArray4 = {"small", "fast", "boat"};
|
The arrays are disjoint!
|
strArray5 = {"north", "south", "east"};
strArray6 = {"west", "north", "east"};
|
The arrays are *not* disjoint !
|
| The java method should accept following input parameters: arr1 (int array), arr2 (int array).
|
| Initialize a variable set1 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 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:
Check if arr2[idx] in already present in set1. If it is present, return false from the program as we have found a common element.
|
| At completion of above loop, if we have not returned after finding a common element, it means the arr1 and arr2 are disjoint. Return true.
|
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;
public class SetArraysCheckDisjoint {
public boolean setArraysCheckDisjoint(String[] arr1, String[] arr2) {
HashSet<String> set1=new HashSet<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])) {
return false;
}
}
return true;
}
public static void main(String[] args) {
SetArraysCheckDisjoint sp=new SetArraysCheckDisjoint();
boolean retVal;
try {
String[] strArray1 = {"hi", "hello", "hola"};
String[] strArray2 = {"good", "better", "best"};
printArraySummary(strArray1, "Original Array1");
printArraySummary(strArray2, "Original Array2");
retVal=sp.setArraysCheckDisjoint(strArray1, strArray2);
if (retVal==true) {
System.out.println("\n The arrays are disjoint !\n");
}else {
System.out.println("\n The arrays are *not* disjoint !\n");
}
String[] strArray3 = {"great", "big", "ship"};
String[] strArray4 = {"small", "fast", "boat"};
printArraySummary(strArray3, "Original Array1");
printArraySummary(strArray4, "Original Array2");
retVal=sp.setArraysCheckDisjoint(strArray3, strArray4);
if (retVal==true) {
System.out.println("\n The arrays are disjoint !\n");
}else {
System.out.println("\n The arrays are *not* disjoint !\n");
}
String[] strArray5 = {"north", "south", "east"};
String[] strArray6 = {"west", "north", "east"};
printArraySummary(strArray5, "Original Array1");
printArraySummary(strArray6, "Original Array2");
retVal=sp.setArraysCheckDisjoint(strArray5, strArray6);
if (retVal==true) {
System.out.println("\n The arrays are disjoint !\n");
}else {
System.out.println("\n The arrays are *not* disjoint !\n");
}
}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;
}
}