Open In App

Check if an array is subset of another array

Last Updated : 28 Aug, 2025
Comments
Improve
Suggest changes
177 Likes
Like
Report

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: true

Input: a[]= [1, 2, 3, 4, 5, 6], b = [1, 2, 4] 
Output: true

Input: a[] = [10, 5, 2, 23, 19], b = [19, 5, 3] 
Output: false

[Naive approach] Using Nested Loops - O(m*n) Time and O(1) Space

The very basic approach is to use two nested loops: the outer loop picks each element from b[], and the inner loop searches for this element in a[] and check for all elements in b[].

C++
#include<bits/stdc++.h>
using namespace std;

bool isSubset(vector<int> & a, vector<int> & b) {
  
    // Iterate over each element in the second array
  int m=a.size(),n=b.size();
    for (int i = 0; i < n; i++) {
        bool found = false;
      
        // Check if the element exists in the first array
        for (int j = 0; j < m; j++) {
            if (b[i] == a[j]) {
                found = true;
                a[j] = -1 ;
                break;
            }
        }
      
        // If any element is not found, return false
        if (!found) return false;
    }
  
    // If all elements are found, return true
    return true;
}

int main() {
    vector<int> a = {11, 1, 13, 21, 3, 7};
    vector<int> b = {11, 3, 7, 1};
  
    if (isSubset(a, b)) {
        cout << "true" << endl;
    } else {
        cout << "false" << endl;
    }

    return 0;
}
Java Python C# JavaScript

Output
true

Time Complexity: O(m*n)
Auxiliary Space: O(1)

[Better Approach] Using Sorting and Two Pointer

Sort both arrays and use two pointers to traverse them. If the current element of a[] is smaller, move the pointer in a. If the elements match, move both pointers. If the current element of a[] is greater than b[], then b[j] is missing in a[], so return false.

C++
#include <bits/stdc++.h>
using namespace std;

bool isSubset(vector<int>& a, vector<int>& b) {
    // Sort both arrays
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());

    int i = 0, j = 0;
    int m = a.size(), n = b.size();

    while (i < m && j < n) {
        if (a[i] < b[j]) {
            i++;   // move in a to catch up
        }
        else if (a[i] == b[j]) {
            i++;
            j++;   // matched one element from b
        }
        else {
            // a[i] > b[j] → means b[j] is missing
            return false;
        }
    }

    return (j == n); // all b[] matched
}

int main() {
    vector<int> a = {11, 1, 13, 21, 3, 7};
    vector<int> b = {11, 3, 7, 1};

    if (isSubset(a, b)) cout << "true\n";
    else cout << "false\n";

    return 0;
}
C Java Python C# JavaScript

Output
true

Time Complexity: O(m log m + n log n)
Auxiliary Space: O(1)

[Expected Approach] Using Hashing- O(m + n) Time and O(m) Space

We can use a hash set to store elements of a[], this will help us in constant time complexity searching. We first insert all elements of a[] into a hash set. Then, for each element in b[], we check if it exists in the hash set.

C++
#include <bits/stdc++.h>
using namespace std;

bool isSubset( vector<int>& a,  vector<int>& b) {

  // Create a hash set and insert all elements of a
    multiset<int> hashSet(a.begin(), a.end());
    
    // Check each element of b in the hash set
    for (int num : b) {
        if (hashSet.find(num) == hashSet.end()) {
            return false;
        }
        hashSet.erase(hashSet.find(num)) ;
    }
    
    // If all elements of b are found in the hash set
    return true;
}

int main() {
    vector<int> a = {1, 2, 3, 4, 5, 6, 7, 8};
    vector<int> b = {1, 2, 3, 1};
    
    if (isSubset(a, b)) {
        cout << "true" << endl;
    } else {
        cout << "false" << endl;
    }
    
    return 0;
}
Java Python C# JavaScript

Output
true

Time Complexity: O(m + n), where m and n are the size of a and b respectively.
Auxiliary Space: O(m)


Array Subset of another Array | DSA Problem

Explore