-
Notifications
You must be signed in to change notification settings - Fork 16
/
Find_subset_array.cpp
42 lines (37 loc) · 1.16 KB
/
Find_subset_array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Java code to find whether an array is subset of
// another array
import java.util.HashSet;
class GFG
{
/* Return true if arr2[] is a subset of arr1[] */
static boolean isSubset(int arr1[], int arr2[], int m,
int n)
{
HashSet<Integer> hset= new HashSet<>();
// hset stores all the values of arr1
for(int i = 0; i < m; i++)
{
if(!hset.contains(arr1[i]))
hset.add(arr1[i]);
}
// loop to check if all elements of arr2 also
// lies in arr1
for(int i = 0; i < n; i++)
{
if(!hset.contains(arr2[i]))
return false;
}
return true;
}
public static void main(String[] args)
{
int arr1[] = {11, 1, 13, 21, 3, 7};
int arr2[] = {11, 3, 7, 1};
int m = arr1.length;
int n = arr2.length;
if(isSubset(arr1, arr2, m, n))
System.out.println("arr2 is a subset of arr1");
else
System.out.println("arr2 is not a subset of arr1");
}
}