| Input Parameters |
Expected outputs |
strArray1 = {"hi", "hello", "hola"};
strArray2 = {"hello", "hola", "hi"};
|
The arrays are equal !
|
strArray1 = {"north", "south", "east"};
strArray2 = {"north", "west", "south"};
|
The arrays are *not* equal !
|
| 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 of arr1.
|
| Check that arr1 and arr2 length ae the same, if not return false as then the arrays cannot be equal.
|
| 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 *not* present, return false from the program as we have found an element that is not common.
|
| At completion of above loop, if we have not returned after finding a non=common element, it means the arr1 and arr2 are equal. 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 SetArraysCheckEquality {
public boolean setArraysCheckEquality(String[] arr1, String[] arr2) {
HashSet<String> set1=new HashSet<String>();
if (arr1.length != arr2.length) {
return false;
}
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) {
SetArraysCheckEquality sp=new SetArraysCheckEquality();
boolean retVal;
try {
String[] strArray1 = {"hi", "hello", "hola"};
String[] strArray2 = {"hello", "hola", "hi"};
printArraySummary(strArray1, "Original Array1");
printArraySummary(strArray2, "Original Array2");
retVal=sp.setArraysCheckEquality(strArray1, strArray2);
if (retVal==true) {
System.out.println("\n The arrays are equal !\n");
}else {
System.out.println("\n The arrays are *not* equal !\n");
}
String[] strArray3 = {"great", "big", "ship"};
String[] strArray4 = {"ship", "great", "big"};
printArraySummary(strArray3, "Original Array1");
printArraySummary(strArray4, "Original Array2");
retVal=sp.setArraysCheckEquality(strArray3, strArray4);
if (retVal==true) {
System.out.println("\n The arrays are equal !\n");
}else {
System.out.println("\n The arrays are *not* equal !\n");
}
String[] strArray5 = {"north", "south", "east"};
String[] strArray6 = {"north", "west", "south"};
retVal=sp.setArraysCheckEquality(strArray3, strArray4);
printArraySummary(strArray5, "Original Array1");
printArraySummary(strArray6, "Original Array2");
retVal=sp.setArraysCheckEquality(strArray5, strArray6);
if (retVal==true) {
System.out.println("\n The arrays are equal !\n");
}else {
System.out.println("\n The arrays are *not* equal !\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;
}
}