Rename js snippets

This commit is contained in:
Angelos Chalaris
2023-05-19 20:23:47 +03:00
parent 82a614e42e
commit 9d032ce05e
305 changed files with 70 additions and 70 deletions

View File

@ -0,0 +1,33 @@
---
title: Array is contained in other array
type: snippet
language: javascript
tags: [array]
author: chalarangelo
cover: island-corridor
dateModified: 2020-10-22T20:23:47+03:00
---
Checks if the elements of the first array are contained in the second one regardless of order.
- 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 number of occurrences 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
```