Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
isSubset of two arrays in JavaScript
In JavaScript, checking if one array is a subset of another means verifying that all elements of the second array exist in the first array. This is a common programming problem that can be solved efficiently using JavaScript's Set data structure.
Understanding the Problem
An array is considered a subset of another array if all its elements are present in the parent array. For example, [2, 4, 6] is a subset of [1, 2, 3, 4, 5, 6] because all elements (2, 4, 6) exist in the larger array.
Using Set for Efficient Lookup
The most efficient approach uses a Set for O(1) average lookup time. We create a Set from the parent array and check if each element of the potential subset exists in the Set.
function isSubset(parentArray, subsetArray) {
const parentSet = new Set(parentArray);
for (let element of subsetArray) {
if (!parentSet.has(element)) {
return false;
}
}
return true;
}
// Test cases
const parent = [1, 2, 3, 4, 5, 6];
const subset1 = [2, 4, 6];
const subset2 = [1, 7, 3];
const subset3 = [];
console.log(isSubset(parent, subset1)); // true
console.log(isSubset(parent, subset2)); // false (7 not in parent)
console.log(isSubset(parent, subset3)); // true (empty array is subset)
true false true
Using every() Method
JavaScript's every() method provides a more concise solution by checking if every element satisfies a condition.
function isSubsetUsingEvery(parentArray, subsetArray) {
const parentSet = new Set(parentArray);
return subsetArray.every(element => parentSet.has(element));
}
// Test with string arrays
const colors = ['red', 'blue', 'green', 'yellow', 'purple'];
const selectedColors1 = ['blue', 'green'];
const selectedColors2 = ['orange', 'blue'];
console.log(isSubsetUsingEvery(colors, selectedColors1)); // true
console.log(isSubsetUsingEvery(colors, selectedColors2)); // false
true false
Comparison of Methods
| Method | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
| Set with for loop | O(n + m) | O(n) | Good |
| Set with every() | O(n + m) | O(n) | Excellent |
| includes() method | O(n × m) | O(1) | Good |
n = parent array length, m = subset array length
Edge Cases
function isSubsetComplete(parentArray, subsetArray) {
// Handle edge cases
if (!Array.isArray(parentArray) || !Array.isArray(subsetArray)) {
throw new Error('Both parameters must be arrays');
}
if (subsetArray.length === 0) return true; // Empty array is always subset
if (parentArray.length === 0) return false; // Non-empty subset of empty array
const parentSet = new Set(parentArray);
return subsetArray.every(element => parentSet.has(element));
}
// Test edge cases
console.log(isSubsetComplete([1, 2, 3], [])); // true
console.log(isSubsetComplete([], [1])); // false
console.log(isSubsetComplete([1, 1, 2], [1, 2])); // true (handles duplicates)
true false true
Conclusion
The Set-based approach with every() method provides the most efficient and readable solution for checking array subsets in JavaScript. It achieves O(n + m) time complexity and handles edge cases gracefully, making it ideal for most practical scenarios.
