Challenge: Find Whether an Array is a Subset of Another Array

Can you find whether a given array is a subset of another by using a built-in Hash Table? A solution is placed in the "solution" section to help you, but we would suggest you try to solve it on your own first.

Problem Statement

In this problem, you have to implement the isSubset() function to take two arrays as input and check whether an array is a subset of another given array.

Note: Both of these arrays represent sets, therefore, they do not contain duplicate values.

Use the built-in HashSet() class. It works like a Hash Table at the back-end and comes in handy during Java interviews.

An illustration of the problem is also provided for your understanding.

Function Prototype:

boolean isSubset(int []arr1,int[]arr2);

Here, arr1 and arr2 are integer arrays.

Output:

It returns true if arr2 is a subset of arr1, or else it returns false

Sample Input

arr1 = [9,4,7,1,-2,6,5]
arr2 = [7,1,-2]

Arrays representing sets.

Sample Output

true

Explanation

[7,1,-2] is present in arr1 from index 2 to 4, therefore arr2 is a subset of arr1.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.