Find whether an array is a subset of another array
Pattern: Hashing and frequency
Count the copies of each value in the first array, then let the second array use them up. Running out means it is not a subset.
No verified practice link yet. The guide below states the problem in full, so you can still solve it here.
The problem
You are given two arrays a1 and a2. Return true if every element of a2 can be matched with its own element of
a1 with the same value, counting repeats (so a2 = [1, 1] needs at least two 1s in a1). Otherwise return false. An
empty a2 is always a subset. For example, [11, 3, 7, 1, 7] is a subset of [11, 7, 1, 13, 21, 3, 7, 3].
This problem is from Love Babbar's 450 sheet, and this guide has no verified link for it yet. The function name
isSubset and the "counting repeats" rule are this guide's own.
Think first
- Is
[1, 1]a subset of[1, 2, 3]under this rule? Would a simple set say yes? - What do you need to know about
a1for each value? - What happens to that knowledge each time
a2uses a value?
Hints
Brute force, and why it fails
For each element of a2, search a1 for an unused matching element and mark it as used: O(n × m). Sorting both arrays and
walking them together is O(n log n + m log m). Counting is O(n + m).
The key insight
"Counting repeats" means each copy can be used once. A map of copies left, decreased as a2 uses them, captures exactly
that.
Dry run
| available | {1: 1, 2: 1, 3: 1} |
|---|
a1 = [1, 2, 3] and a2 = [1, 1]. First count what a1 has. One copy each of 1, 2 and 3.
The plan
available = count of each value in a1
for x in a2:
if available[x] == 0: # 0 if missing
return false
available[x] = available[x] - 1
return true
Complexity
- Time
O(n + m)- Space
O(n)
One pass over each array. The map holds up to n distinct values of a1.
Edge cases
- Empty
a2: true. a2longer thana1: it must run out somewhere, so false.- A value of
a2that never appears ina1: false at once.
I've built my logic, show the code
Java
import java.util.*;
class Solution {
public boolean isSubset(int[] a1, int[] a2) {
Map<Integer, Integer> available = new HashMap<>(); // value -> copies left in a1
for (int x : a1) {
available.put(x, available.getOrDefault(x, 0) + 1);
}
for (int x : a2) {
int left = available.getOrDefault(x, 0);
if (left == 0) {
return false; // a1 has no copy left for this one
}
available.put(x, left - 1);
}
return true;
}
}C++
class Solution {
public:
bool isSubset(vector<int>& a1, vector<int>& a2) {
unordered_map<int, int> available; // value -> copies left in a1
for (int x : a1) {
available[x]++;
}
for (int x : a2) {
auto found = available.find(x);
if (found == available.end() || found->second == 0) {
return false; // a1 has no copy left for this one
}
found->second--;
}
return true;
}
};Python
class Solution:
def isSubset(self, a1, a2):
available = {} # value -> copies left in a1
for x in a1:
available[x] = available.get(x, 0) + 1
for x in a2:
if available.get(x, 0) == 0:
return False # a1 has no copy left for this one
available[x] -= 1
return TrueCommon mistakes
- Using a set of
a1's values, which accepts[1, 1]against a single 1. - Forgetting to decrease the count after a match.
- Swapping the roles of the two arrays.