-
Notifications
You must be signed in to change notification settings - Fork 634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
阿里算法题:编写一个函数计算多个数组的交集 #10
Comments
var intersection = function (nums1, nums2) {
let arr1 = Array.from(new Set(nums1));
let arr2 = Array.from(new Set(nums2));
let arr3 = new Array();
for (let [v, k] of arr1.entries()) {
if (arr2.includes(k)) {
arr3.push(k)
}
}
return arr3;
};
// 忽略我的命名...... |
const intersection = (arrs) => {
/** @returns { Set } intersection 两个集合的交集*/
const intersectTwoSets = (set1, set2) => {
const result = new Set(set1)
for (let item of result) {
if (!set2.has(item)) {
result.delete(item)
}
}
return result
}
/** @description 所有数组的交集 */
const resultSet = (
arrs
.map(arr => new Set(arr))
// O(N * M)
.reduce(intersectTwoSets)
)
return Array.from(resultSet)
} |
万能的 const getIntersection = (...arrs) => {
return Array.from(new Set(arrs.reduce((total, arr) => {
return arr.filter(item => total.includes(item));
})));
} |
function intersect(arrs) {
let obj = {},
result = {};
for (let i = 0; i < arrs[0].length; i++) {
obj[i] = 1;
}
for (let i = 1; i < arrs.length; i++) {
result = {};
arrs[i].forEach(element => {
obj[element] ? (result[element] = 1) : "";
});
obj = result;
}
return Object.keys(obj);
} |
一行代码搞定 let intersection = (list , ...args) => list.filter( item => args.every( list => list.includes( item )))
console.log( intersection( [ 2,1 ], [ 2,3 ] ) ) // [ 2 ]
console.log( intersection( [ 2,1 ], [ 4,3 ] ) ) // [ ] |
|
|
在原先两个数组求交集的基础上使用reduce
合并之后就是
|
|
function intersection(...arr) {
let result = [];
arr.forEach(arr => {
if( arr instanceof Array ){
// 如果是第一个数组,则直接将该数组去重然后 push 到目标数组中
if( !result.length ) {
result.push(...new Set(arr));
return ;
}
// 如果是第二个及以后的数组,则先去重然后用目标数组过滤
const tempSet = new Set(arr);
result = result.filter(item=>tempSet.has(item));
}
});
return result;
} |
var intersection = function (...arrs) {
let res= arrs[0]
for (let i = 1; i < arrs.length; i++) {
res = res.filter(item=> arrs[i].includes(item))
}
return [...new Set(res)]
} |
function intersection(){
return Array.prototype.slice.call(arguments).reduce((arr, item) => {
return item.filter(key => arr.includes(key));
})
} |
使用 reducer 函数 const intersection = function(...args) {
if (args.length === 0) {
return []
}
if (args.length === 1) {
return args[0]
}
return [...new Set(args.reduce((result, arg) => {
return result.filter(item => arg.includes(item))
}))]
}; |
|
var intersection = function() {
|
需要注意的地方:
|
function merge(){ |
1function twoIntersection(p, n) {
const map = {}
const res = []
p.forEach((key) => {
map[key] = 1
})
n.forEach((key) => {
if (map[key]) {
map[key]++
if (map[key] === 2) {
res.push(key)
}
}
})
return res
}
const intersection = (...arrs) => arrs.reduce(twoIntersection) 2const intersection = (first, ...rest) => [
...new Set(first.filter((item) => rest.every((arr) => arr.includes(item)))),
] |
const intersection2 = (...arrays) => {
if (arrays.length === 0) {
return []
}
if (arrays.length === 1) {
return arrays[ 0 ]
}
return [ ...new Set(arrays.reduce((result, array) => {
return result.filter((it) => array.includes(it))
}))]
} |
function intersection (arrays) {
let result = []
let resMap = {}
let tempArrays = arrays.flat(2)
for (let num of tempArrays) {
if (resMap[num]) {
result.push(num)
} else {
resMap[num] = num
}
}
return [...new Set(result)]
} |
` ` |
function intersection(arrs) {
function intersectionTwo(nums1, nums2) {
return Array.from(new Set(nums1.filter(v => nums2.includes(v))))
}
return arrs.reduce(intersectionTwo)
} |
// 多个数组取交集(三个及以上) |
It's helpful for me. |
function getArr(...argument) {
if (argument.length === 0) {
return []
}
return calc(argument)
}
function calc(arr) {
const len = arr.length
if (len === 1) {
return arr[0]
}
const mid = Math.floor(len / 2)
const right = arr.slice(0, mid)
const left = arr.slice(mid, len)
return calcTwo(calc(right), calc(left))
}
function calcTwo(arr1, arr2) {
const len1 = arr1.length
const len2 = arr2.length
if (len2 > len1) {
[arr1, arr2] = [arr2, arr1]
}
return Array.from(new Set(arr1.filter(v => arr2.includes(v))))
} |
对不起,暂时不能亲自收到你的来信。当我看到了,我會尽快回复。
|
这是来自QQ邮箱的假期自动回复邮件。你好,我最近正在休假中,无法亲自回复你的邮件。我将在假期结束后,尽快给你回复。
|
编写一个函数计算多个数组的交集 | 前端瓶子君 |
对不起,暂时不能亲自收到你的来信。当我看到了,我會尽快回复。
|
这是来自QQ邮箱的假期自动回复邮件。你好,我最近正在休假中,无法亲自回复你的邮件。我将在假期结束后,尽快给你回复。
|
要求:
输出结果中的每个元素一定是唯一的
The text was updated successfully, but these errors were encountered: