Checks if the elements of the first array are contained in the second one regardless of order.
for...of
loop over a Set
created from the first array.Array.prototype.some()
to check if all distinct values are contained in the second array.Array.prototype.filter()
to compare the number of occurrences of each distinct value in both arrays.false
if the count of any element is greater in the first array than the second one, true
otherwise.const isContainedIn = (a, b) => {
for (const v of new Set(a)) {
if (
!b.some(e => e === v) ||
a.filter(e => e === v).length > b.filter(e => e === v).length
)
return false;
}
return true;
};
isContainedIn([1, 4], [2, 4, 1]); // true
JavaScript, Array
Checks if two arrays contain the same elements regardless of order.
JavaScript, Array
Generates all permutations of an array's elements (contains duplicates).
JavaScript, Array
Checks if all elements in an array are unique, based on the provided mapping function.