Add isContained
This commit is contained in:
26
snippets/isContainedIn.md
Normal file
26
snippets/isContainedIn.md
Normal file
@ -0,0 +1,26 @@
|
||||
---
|
||||
title: isContainedIn
|
||||
tags: array,intermediate
|
||||
---
|
||||
|
||||
Returns `true` if the elements of the first array are contained in the second one regardless of order, `false` otherwise.
|
||||
|
||||
Use a `for...of` loop over a `Set` created from the first array.
|
||||
Use `Array.prototype.some()` to check if all distinct values are contained in the second array, use `Array.prototype.filter()` to compare the amount of occurences of each distinct value in both arrays.
|
||||
Return `false` if the count of any element is greater in the first array than the second one, `true` otherwise.
|
||||
|
||||
```js
|
||||
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;
|
||||
}
|
||||
```
|
||||
|
||||
```js
|
||||
isContainedIn([1, 4], [2, 4, 1]); // true
|
||||
```
|
||||
17
test/isContainedIn.test.js
Normal file
17
test/isContainedIn.test.js
Normal file
@ -0,0 +1,17 @@
|
||||
const {isContainedIn} = require('./_30s.js');
|
||||
|
||||
test('isContainedIn is a Function', () => {
|
||||
expect(isContainedIn).toBeInstanceOf(Function);
|
||||
});
|
||||
test('returns true for arrays with same contents', () => {
|
||||
expect(isContainedIn([1, 2, 3], [2, 3, 1])).toBeTruthy();
|
||||
});
|
||||
test('returns true for arrays with correct contents', () => {
|
||||
expect(isContainedIn([1, 2], [2, 3, 1])).toBeTruthy();
|
||||
});
|
||||
test('returns false for arrays with different contents', () => {
|
||||
expect(isContainedIn([1, 4], [2, 3, 1])).toBeFalsy();
|
||||
});
|
||||
test('returns false for arrays with different contents', () => {
|
||||
expect(isContainedIn([1, 2, 3], [2, 1, 6])).toBeFalsy();
|
||||
});
|
||||
Reference in New Issue
Block a user