Given two integer arrays arr1 and arr2, check if all elements of arr2 are contained in arr1.
| Input Parameters |
Expected outputs |
intArray1 = {1,33,54,56,22,98, 49, 99, 23, 27, 74};
intArray2 = {22, 23, 27};
|
Array1 contains all Array2 elements!
|
intArray5 = {45, 56, 86, 66, 34, 85, 36, 67, 49, 98, 56};
intArray6 = {56, 34, 89};
|
Array1 does *not* contain all Array2 elements !
|
| The java method should accept following input parameters: arr1 (int array), arr2 (int array).
|
| Initialize a variable set1 of type HashSet to hold the input elements of arr1.
|
| 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 of arr2 that is not present in arr1.
|
| At completion of above loop, if we have not returned after finding a non-common element, it all elements of arr2 are contained in arr1. 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 SetArraysContains {
public boolean setArraysContains(int[] arr1, int[] arr2) {
HashSet<Integer> set1=new HashSet<Integer>();
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) {
SetArraysContains sp=new SetArraysContains();
boolean retVal;
try {
int[] intArray1 = {1,33,54,56,22,98, 49, 99, 23, 27, 74};
int[] intArray2 = {22, 23, 27};
printArraySummary(intArray1, "Original Array1");
printArraySummary(intArray2, "Original Array2");
retVal=sp.setArraysContains(intArray1, intArray2);
if (retVal==true) {
System.out.println("\n Array2 contains all Array1 elements!\n");
}else {
System.out.println("\n Array2 does *not* contain all Array1 elements !\n");
}
int[] intArray3 = {44, 98, 35, 90, 21, 87, 94, 45, 66, 89, 56, 45, 9, 67, 54};
int[] intArray4 = {98, 67};
printArraySummary(intArray3, "Original Array1");
printArraySummary(intArray4, "Original Array2");
retVal=sp.setArraysContains(intArray3, intArray4);
if (retVal==true) {
System.out.println("\n Array2 contains all Array1 elements!\n");
}else {
System.out.println("\n Array2 does *not* contain all Array1 elements !\n");
}
int[] intArray5 = {45, 56, 86, 66, 34, 85, 36, 67, 49, 98, 56};
int[] intArray6 = {56, 34, 89};
printArraySummary(intArray5, "Original Array1");
printArraySummary(intArray6, "Original Array2");
retVal=sp.setArraysContains(intArray5, intArray6);
if (retVal==true) {
System.out.println("\n Array2 contains all Array1 elements!\n");
}else {
System.out.println("\n Array2 does *not* contain all Array1 elements !\n");
}
}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;
}
}