Find whether an array is subset of another array using Map
Last Updated :
08 Jun, 2021
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.
C++
// 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;
}
Java
// Java program to check if an array is
// subset of another array
import java.util.*;
class GFG
{
// Function to check if an array is
// subset of another array
static int isSubset(int a[], int b[], int m, int n)
{
// map to store the values of array a[]
HashMap<Integer, Integer> mp1 = new
HashMap<Integer, Integer>();
for (int i = 0; i < m; i++)
if (mp1.containsKey(a[i]))
{
mp1.put(a[i], mp1.get(a[i]) + 1);
}
else
{
mp1.put(a[i], 1);
}
// 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.containsKey(b[i]))
{
f = 1;
break;
}
// if if b[i] is present in map
// decrement by one
else
{
mp1.put(b[i], mp1.get(b[i]) - 1);
if (mp1.get(b[i]) == 0)
mp1.remove(b[i]);
}
}
return f;
}
// Driver code
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)!=1)
System.out.print("arr2[] is subset of arr1[] ");
else
System.out.print("arr2[] is not a subset of arr1[]");
}
}
// This code is contributed by Rajput-Ji
Python
# Python program to check if an array is
# subset of another array
# Function to check if an array is
# subset of another array
def isSubset(a, b, m, n) :
# map to store the values of array a
mp1 = {}
for i in range(m):
if a[i] not in mp1:
mp1[a[i]] = 0
mp1[a[i]] += 1
# flag value
f = 0
for i in range(n):
# if b[i] is not present in map
# then array b can not be a
# subset of array a
if b[i] not in mp1:
f = 1
break
# if if b[i] is present in map
# decrement by one
else :
mp1[b[i]] -= 1
if (mp1[b[i]] == 0):
mp1.pop(b[i])
return f
# Driver code
arr1 = [11, 1, 13, 21, 3, 7 ]
arr2 = [11, 3, 7, 1 ]
m = len(arr1)
n = len(arr2)
if (not isSubset(arr1, arr2, m, n)):
print("arr2[] is subset of arr1[] ")
else:
print("arr2[] is not a subset of arr1[]")
# This code is contributed by Shubhamsingh10
C#
// C# program to check if an array is
// subset of another array
using System;
using System.Collections.Generic;
class GFG
{
// Function to check if an array is
// subset of another array
static int isSubset(int []a, int []b, int m, int n)
{
// map to store the values of array []a
Dictionary<int, int> mp1 = new
Dictionary<int, int>();
for (int i = 0; i < m; i++)
if (mp1.ContainsKey(a[i]))
{
mp1[a[i]] = mp1[a[i]] + 1;
}
else
{
mp1.Add(a[i], 1);
}
// 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.ContainsKey(b[i]))
{
f = 1;
break;
}
// if if b[i] is present in map
// decrement by one
else
{
mp1[b[i]] = mp1[b[i]] - 1;
if (mp1[b[i]] == 0)
mp1.Remove(b[i]);
}
}
return f;
}
// Driver code
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) != 1)
Console.Write("arr2[] is subset of arr1[] ");
else
Console.Write("arr2[] is not a subset of arr1[]");
}
}
// This code is contributed by PrinciRaj1992
JavaScript
<script>
// JavaScript program to check if an array is
// subset of another array
// Function to check if an array is
// subset of another array
function isSubset(a, b, m, n) {
// map to store the values of array a[]
let mp = new Map();
for (let i = 0; i < m; i++) {
if (mp.has(a[i])) {
mp.set(a[i], mp.get(a[i]) + 1)
} else {
mp.set(a[i], 1)
}
}
// flag value
let f = 0;
for (let 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 (!mp.has(b[i])) {
f = 1;
break;
}
// if if b[i] is present in map
// decrement by one
else {
mp.set(b[i], mp.get(b[i]) - 1);
if (mp.get(b[i]) == 0)
mp.delete(b[i]);
}
}
return f;
}
// Driver code
let arr1 = [11, 1, 13, 21, 3, 7];
let arr2 = [11, 3, 7, 1];
let m = arr1.length;
let n = arr2.length;
if (!isSubset(arr1, arr2, m, n))
document.write("arr2[] is subset of arr1[] ");
else
document.write("arr2[] is not a subset of arr1[]");
// This code is contributed by gfgking
</script>
Output: arr2[] is subset of arr1[]
Time Complexity: O (n)
Similar Reads
How to check whether an array is subset of another array using JavaScript ? The task is to check whether an array is a subset of another array with the help of JavaScript. There are a few approaches, we will discuss below: Approaches:using JavaScript array.every() methodusing JavaScript array.some() methodApproach 1: Using JavaScript array.every() methodThis approach checks
2 min read
Check if an array is subset of another array Given two arrays a[] and b[] of size m and n respectively, the task is to determine whether b[] is a subset of a[]. Both arrays are not sorted, and elements are distinct.Examples: Input: a[] = [11, 1, 13, 21, 3, 7], b[] = [11, 3, 7, 1] Output: trueInput: a[]= [1, 2, 3, 4, 5, 6], b = [1, 2, 4] Output
13 min read
Map elements of an array to elements of another array Given two arrays A and B of positive integers, elements of array B can be mapped to elements of array A only if both the elements have same value. The task is to compute the positions in array A to which elements of array B will be mapped. Print NA if mapping for a particular element cannot be done.
6 min read
Check whether bitwise AND of a number with any subset of an array is zero or not Given an array and a Number N. The task is to check whether there exists any subset of this array such that the bitwise AND of this subset with N is zero. Examples: Input : arr[] = {1, 2, 4} ; N = 3 Output : YES Explanation: The subsets are: (1, 2 ), (1, 4), (1, 2, 4) Input : arr[] = {1, 1, 1} ; N =
6 min read
Find elements which are present in first array and not in second Given two arrays, the task is that we find numbers which are present in first array, but not present in the second array. Examples : Input : a[] = {1, 2, 3, 4, 5, 10}; b[] = {2, 3, 1, 0, 5};Output : 4 10 4 and 10 are present in first array, butnot in second array.Input : a[] = {4, 3, 5, 9, 11}; b[]
14 min read
Check if each element of an Array is the Sum of any two elements of another Array Given two arrays A[] and B[] consisting of N integers, the task is to check if each element of array B[] can be formed by adding any two elements of array A[]. If it is possible, then print âYesâ. Otherwise, print âNoâ. Examples: Input: A[] = {3, 5, 1, 4, 2}, B[] = {3, 4, 5, 6, 7} Output: Yes Explan
6 min read
Find the missing value from Array B formed by adding some value X to Array A Given two arrays arr1[] and arr2[] of size N and N - 1 respectively. Each value in arr2[] is obtained by adding a hidden value say X to any arr1[i] for N-1 times, which means for any i, arr1[i]+X is not included in arr2[] that is the missing value in arr2[]. The task is to find both the hidden value
10 min read
Check if an array element is concatenation of two elements from another array Given two arrays arr[] and brr[] consisting of N and M positive integers respectively, the task is to find all the elements from the array brr[] which are equal to the concatenation of any two elements from the array arr[]. If no such element exists, then print "-1". Examples: Input: arr[] = {2, 34,
8 min read
Check whether for all pair (X, Y) of given Array floor of X/Y is also present Given an array arr[] of size N and an integer K, denoting the maximum value an array element can have, the task is to check if for all possible pairs of elements (X, Y) where Xâ¥Y, âX/Yâ (X divided by Y with rounding down) is also present in this array. Examples: Input: N = 3, K = 5, arr[]={1, 2, 5}O
10 min read
Check if all K-length subset sums of first array greater than that of the second array Given two arrays A[] and B[] of size N and an integer K, the task is to check if all possible subset-sums of subsets of size K of the array A[] are greater than that of the array B[] or not. If found to be true, then print "YES". Otherwise, print "NO". Examples: Input: A[] = {12, 11, 10, 13}, B[] =
7 min read