VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-whether-an-array-is-subset-of-another-array-set-1/

⇱ Check if an array is subset of another array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if an array is subset of another array

Last Updated : 4 Feb, 2026

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[].


Output
true

[Better Approach] Using Sorting and Two Pointer - O(m log m + n log n) Time and O(1) space

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.


Output
true

[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.


Output
true
Comment