# C++ Program to check whether an array is subset of another array - Hashing

In this program we will take two arrays and check whether that array 2 is a subset of array 1 or not. C++ program along with output is also given below.

For example:

1.  Let A1[] ={ 12, 14, 56, 6, 8 },   Let A2[]={ 14, 8, 6}
Array A2 is subset of A1

1.  Let A1[] ={ 12, 14, 56, 6, 8 },   Let A2[]={ 14, 8, 7}
Array A2 is not a subset of A1 because 7 is  not present in the A1.

Algorithm:

1.   Create two hash table for array 2 and array 1 where value of arrays will be key and their
count will be value corresponding to that key.
2.   Traverse the array 2 and check the number of occurrence of each element of array 2 in array 1.
3.   If number of occurrences are same then return true else return false.

PROGRAM:

```#include <stdio.h>
#include <unordered_map>
using namespace std;
unordered_map <int,int> map1;
unordered_map <int,int> map2;

int check(int n2,int arr2[]);
int main()
{
int arr1,arr2,n1,n2;

map1.reserve(10);
map2.reserve(10);

printf("\nEnter the number of elements in array 1: ");
scanf("%d",&n1);
printf("\nEnter the elements of array 1: \n");
for(int i=0;i<n1;i++)
{
scanf("%d",&arr1[i]);
map1[arr1[i]]++;
}

printf("\nEnter the number of elements in array 2: ");
scanf("%d",&n2);
printf("\nEnter the elements of array 2:\n");
for(int i=0;i<n2;i++)
{
scanf("%d",&arr2[i]);
map2[arr2[i]]++;
}

if(check(n2,arr2))
{
printf("\nArray 2 is an subset of Array 1\n");
}
else
{
printf("\nArray 2 is not subset of Array 1\n");
}
return 0;
}
int check(int n2,int array2[])
{
for(int i=0;i<n2;i++)
{
if(!map1[array2[i]])
{
return 0;
}
}
return 1;
}```

OUTPUT:

#### Zaid Khan Balas

this code returns false when duplicate values are present in arr1[] Balas

if the code instead of incrementing the mapped values who set it as a constant value,then the bug is solved. Balas

Correction has been made..See the line number 47...I wonder how I made that kind of silly mistake...Anyway thanks you to point out that bugg..@mayank Balas
This comment has been removed by the author. Balas Balas