# Find whether an array is subset of another array using Map

Given two arrays: arr1[0..m-1] and arr2[0..n-1]. Find whether arr2[] is a subset of arr1[] or not. Both the arrays are not in sorted order. It may be assumed that elements in both arrays are distinct.

**Examples:**

Input:arr1[] = {11, 1, 13, 21, 3, 7}, arr2[] = {11, 3, 7, 1}Output:arr2[] is a subset of arr1[]Input:arr1[] = {1, 2, 3, 4, 5, 6}, arr2[] = {1, 2, 4}Output:arr2[] is a subset of arr1[]Input:arr1[] = {10, 5, 2, 23, 19}, arr2[] = {19, 5, 3}Output:arr2[] is not a subset of arr1[]

**Simple Approach:** A simple approach is to run two nested loops. The outer loop picks all the elements of B[] one by one. The inner loop linearly searches for the element picked by the outer loop in A[]. If all elements are found, then print Yes, else print No. You can check the solution here.

**Efficient Approach:** Create a map to store the frequency of each distinct number present in A[]. Then we will check if each number of B[] is present in map or not. If present in the map, we will decrement the frequency value for that number by one and check for the next number. If map value for any number becomes zero, we will erase it from the map. If any number of B[] is not found in the map, we will set the flag value and break the loops and print No. Otherwise, we will print Yes.

Below is the C++ implementation of the above approach:

`// C++ program to check if an array is ` `// subset of another array ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check if an array is ` `// subset of another array ` ` ` `int` `isSubset(` `int` `a[], ` `int` `b[], ` `int` `m, ` `int` `n) ` `{ ` ` ` ` ` `// map to store the values of array a[] ` ` ` `map<` `int` `, ` `int` `> mp1; ` ` ` ` ` `for` `(` `int` `i = 0; i < m; i++) ` ` ` `mp1[a[i]]++; ` ` ` ` ` `// flag value ` ` ` `int` `f = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `// if b[i] is not present in map ` ` ` `// then array b[] can not be a ` ` ` `// subset of array a[] ` ` ` ` ` `if` `(mp1.find(b[i]) == mp1.end()) { ` ` ` `f = 1; ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `// if if b[i] is present in map ` ` ` `// decrement by one ` ` ` `else` `{ ` ` ` `mp1[b[i]]--; ` ` ` ` ` `if` `(mp1[b[i]] == 0) ` ` ` `mp1.erase(mp1.find(b[i])); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `f; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr1[] = { 11, 1, 13, 21, 3, 7 }; ` ` ` `int` `arr2[] = { 11, 3, 7, 1 }; ` ` ` ` ` `int` `m = ` `sizeof` `(arr1) / ` `sizeof` `(arr1[0]); ` ` ` `int` `n = ` `sizeof` `(arr2) / ` `sizeof` `(arr2[0]); ` ` ` ` ` `if` `(!isSubset(arr1, arr2, m, n)) ` ` ` `cout<<` `"arr2[] is subset of arr1[] "` `; ` ` ` `else` ` ` `cout<<` `"arr2[] is not a subset of arr1[]"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

arr2[] is subset of arr1[]

**Time Complexity:** O (n)

## Recommended Posts:

- Find whether an array is subset of another array | Added Method 3
- Find if there is any subset of size K with 0 sum in an array of -1 and +1
- Find the sum of maximum difference possible from all subset of a given array.
- Find all distinct subset (or subsequence) sums of an array
- Find all distinct subset (or subsequence) sums of an array | Set-2
- Find the smallest positive integer value that cannot be represented as sum of any subset of a given array
- Array with GCD of any of its subset belongs to the given array
- Find original array from encrypted array (An array of sums of other elements)
- Find an element in array such that sum of left array is equal to sum of right array
- Subset array sum by generating all the subsets
- Minimum value among AND of elements of every subset of an array
- Largest divisible subset in array
- Maximum product subset of an array
- Minimum product subset of an array
- Check whether bitwise AND of a number with any subset of an array is zero or not

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.