DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Javascript: Check if an Array is a Subset of Another Array
Johnny Simpson
Johnny Simpson

Posted on • Originally published at fjolt.com

Javascript: Check if an Array is a Subset of Another Array

There are many use cases where knowing if an array is a subset of another can be quite useful - and although usually immediately obvious to the human eye, it can be difficult to evaluate in code.

In the code below, arr2 is a subset of arr1, but arr3 is not.

let arr1 = [ 'a', 'b', 'c' ];
let arr2 = [ 'b', 'c' ];
let arr3 = [ 'c', 'd' ];
Enter fullscreen mode Exit fullscreen mode

If we want to find out if an array is a subset of another array, the best way to evaluate this is by using the array every method. This method iterates through each element in an array and performs a test on it. If every element in the array passes that test, then the overall every method will return true.

For each evaluation, we have a "parent" array, and a "subset" array - where we want to check if the "subset" array is fully contained within the "parent" array.

To evaluate if one array is a subset of another, we can run every on the each element on the "subset" array. Within the every function, we can test if the "parent" array contains each element in the "subset" array. If it does, then every will return true. Otherwise, it'll return false.

This can be accomplished easily with a function like the one shown below - checkSubset will return true should the subsetArray be fully contained within the parentArray:

let arr1 = [ 'a', 'b', 'c' ];
let arr2 = [ 'b', 'c' ];
let arr3 = [ 'c', 'd' ];

let checkSubset = (parentArray, subsetArray) => {
    return subsetArray.every((el) => {
        return parentArray.includes(el)
    })
}

checkSubset(arr1, arr2); // returns true
checkSubset(arr1, arr3); // returns false
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
t0nyba11 profile image
Tony B • Edited on

As the arrays get larger, you might want something not O(n^2) complexity.

let checkSubset = (parentArray, subsetArray) => {
  let set = new Set(parentArray);
  return subsetArray.every(x => set.has(x));
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
smpnjn profile image
Johnny Simpson

this is a great way to use Set(), thanks!

Collapse
 
revenity profile image
Revenity • Edited on

You don't want to search at the beginning of the array every time you search an element in a subarray to make sure it exists in the array right?

function isSub<T = any>(parent: T[], sub: T[]) {
    let currentIndex = -1;
    for (const element of sub) 
        while (parent[++currentIndex] != element) 
            if (currentIndex >= parent.length)
                return false;
    return true;
}
Enter fullscreen mode Exit fullscreen mode

typescript

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!