第 142 题:算法题 - 求多个数组之间的交集
本文介绍了JS数组交集、并集、差集,分享给大家,具体如下:由于下面会用到 ES5 的方法,低版本会存在兼容,先应添加对应的 polyfill
Array.prototype.indexOf = Array.prototype.indexOf || function (searchElement, fromIndex) { var index = -1; fromIndex = fromIndex * 1 || 0; for (var k = 0, length = this.length; k < length; k++) { if (k >= fromIndex && this[k] === searchElement) { index = k; break; } } return index; }; Array.prototype.filter = Array.prototype.filter || function (fn, context) { var arr = []; if (typeof fn === "function") { for (var k = 0, length = this.length; k < length; k++) { fn.call(context, this[k], k, this) && arr.push(this[k]); } } return arr; };
依赖数组去重方法:
// 数组去重 Array.prototype.unique = function() { var n = {}, r = []; for (var i = 0; i < this.length; i++) { if (!n[this[i]]) { n[this[i]] = true; r.push(this[i]); } } return r; }
交集
交集元素由既属于集合A又属于集合B的元素组成
Array.intersect = function(arr1, arr2) { if(Object.prototype.toString.call(arr1) === "[object Array]" && Object.prototype.toString.call(arr2) === "[object Array]") { return arr1.filter(function(v){ return arr2.indexOf(v)!==-1 }) } } // 使用方式 Array.intersect([1,2,3,4], [3,4,5,6]); // [3,4]
并集
并集元素由集合A和集合B中所有元素去重组成
Array.union = function(arr1, arr2) { if(Object.prototype.toString.call(arr1) === "[object Array]" && Object.prototype.toString.call(arr2) === "[object Array]") { return arr1.concat(arr2).unique() } } // 使用方式 Array.union([1,2,3,4], [1,3,4,5,6]); // [1,2,3,4,5,6]
差集
A的差集:属于A集合不属于B集合的元素
B的差集:属于B集合不属于A集合的元素
Array.prototype.minus = function(arr) { if(Object.prototype.toString.call(arr) === "[object Array]") { var interArr = Array.intersect(this, arr);// 交集数组 return this.filter(function(v){ return interArr.indexOf(v) === -1 }) } } // 使用方式 var arr = [1,2,3,4]; arr.minus([2,4]); // [1,3]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
const p1 = [
{
id: 123
},
{
id: 124
},
{
id: 125
},
]
const p2 = [
{
id: 124
},
{
id: 125
},
{
id: 125
},
{
id: 126
},
]
const p3 = [
{
id: 125
},
{
id: 128
},
{
id: 128
},
]
let a = intersections([p1, p2, p3])
console.log(a)
var arr=[1,2,3]
var arr2=[2,3,4,5]
var arr3 = [3,4,5,6]
var arr4 = [6,4,1,2]
function selectArr(arr1,arr2){
var narr = [];
for(var i = 0; i<arr1.length; i++){
for(var j = 0; j<arr2.length; j++){
if(arr1[i]===arr2[j]&&narr.indexOf(arr2[j])==-1){
narr.push(arr2[j])
}
}
}
return narr
}
function setArr(...arg){
if(arg.length===0){
return false
}
if(arg.length===1){
return arg[0]
}
if(arg.length>1){
for(var i = 1; i<arg.length; i++){
arg[i] = selectArr(arg[i-1],arg[i])
}
}
return arg[arg.length-1];
}
console.log(setArr(arr2,arr3))
如果是对象数组上面是不是好多都不管用了
Clarify
Since the description kind of loose, So Let's make it clear.
Suppose that, all the list are unique , eg: [1,2,2,3] is not allow.Otherwise the solution could be quiet ugly.
Concise Solution -
reduce
initially, we can set the answer to be the very first item of the list.
eg: [1,2,3], [1,2,3,5], [1] , we take [1,2,3] as the initial value(the answer can't be greater than it). then we keep
filtering
repeatedlyMore efficient and Easy to understand
repeating count
, store into Map as [k=item, v=count]Thanks for your reading, if like it, Give Me a thumb up
处理数组
处理数组和类数组(有iterable接口的数据结构)
输入的数组有可能出现重复数据,需要多一步判断;如果输入的是集合,就不用这么复杂了
function intersection(...args){
if(args.length == 0) return [];
if(args.length == 1) return args[0];
let arr = [];
args.forEach((item)=>{
arr = arr.concat([...item]);
})
// 并集
// return new Set(arr);
}
intersection([1,2,3],[2,3]) // [2,3]
let array1 = [1,2,3,4,5,6];
let array2 = [2,3,4,5,6,8,9];
let array3 = [4,5,6,7,8,9]
console.log(getIntersection(array1, array2, array3));
function getIntersection(){
let result = [];
let obj = {};
let array = [].slice.apply(arguments)
for(let i = 0; i < array.length; i++) {
let arrayItem = array[i]
for(let n = 0; n < arrayItem.length; n++) {
if (obj[arrayItem[n]]) {
obj[arrayItem[n]] = obj[arrayItem[n]] + 1
} else {
obj[arrayItem[n]] = 1
}
}
};
for(let item in obj) {
if(obj[item] > 1) {
result.push(item)
}
};
return result;
};
let a = new Set([1, 2, 3]);
let b = new Set([4, 3, 2]);
let intersect = new Set([...a].filter(x => b.has(x)));
// set {2, 3}
142