Created
September 29, 2020 19:53
-
-
Save ArtemVeremienko/e48193f600f4c6c3ed09b0250fd9085b to your computer and use it in GitHub Desktop.
Функция, которая создаёт массив из уникальных значений, которые есть в каждом из предоставленных массивов.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Описание задачи: Напишите функцию, которая создаст массив из уникальных значений, которые есть в каждом из предоставленных массивов. | |
* Ожидаемый результат: ([1, 2], [2, 3]) => [2] | |
* Сложность задачи: 4 of 5 | |
* @param {?} arrays - Массив примитивных значений | |
* @returns {Array} | |
*/ | |
const intersection = (...arrays) => { | |
let result = arrays[0].filter(el => arrays[1].includes(el)) | |
if (arrays.length > 2) intersection(result, ...arrays.slice(2)) | |
return [...new Set(result)]; | |
} | |
const arr1 = [1, 2]; | |
const arr2 = [2, 3]; | |
const arr3 = ['a', 'b']; | |
const arr4 = ['b', 'c']; | |
const arr5 = ['b', 'e', 'c']; | |
const arr6 = ['b', 'b', 'e']; | |
const arr7 = ['b', 'c', 'e']; | |
const arr8 = ['b', 'e', 'c']; | |
console.log(intersection(arr1, arr2)) // [2] | |
console.log(intersection(arr3, arr4, arr5, arr3, arr4, arr5)) // ['b'] | |
console.log(intersection(arr6, arr7, arr8, arr7, arr8)) // ['b', 'e'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment