Open In App

Check if an array is subset of another array

Last Updated : 31 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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;
                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;
}
C 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. Move the pointer in b[] if a[]’s element is smaller. If a[]’s element is larger, b’s element isn’t 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;

    // Traverse both arrays using two pointers
    while (i < a.size() && j < b.size()) {
        if (a[i] < b[j]) {
            i++;
        }
        else if (a[i] == b[j]) {
            i++;
            j++;
        }
        else {
            // If element in b is not found in a
            return false;
        }
    }

    // If we have traversed all elements in b, it is a
    // subset
    return (j == b.size());
}

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;
}
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
    unordered_set<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;
        }
    }
    
    // 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)



Next Article

Similar Reads

three90RightbarBannerImg