-
Notifications
You must be signed in to change notification settings - Fork 1
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
第二十八题:实现数组的并、交、差、补集这四个方法 #28
Labels
Comments
1 实现一个去重算法, a,b 2个 concat 后 形成新数组,去重就为并集合 |
实现数组的并、交、差、补集四个方法释意并集: 若A、B是集合,那么并集就是所有A的元素和所有B的元素 交集: 若A、B是集合,那么所有集合A中,集合B中所共有的元素组成的集合,叫做交集 补集:设S是一个集合,A是S的一个子集,由S中所有不属于A元素组成的集合,叫做子集A在集合S中的绝对补集。补集分为绝对补集和相对补集。 差集: 有两个集合,集合A、集合B,差集就是集合A-集合B 差集和补集的区别:补集要求子集A必须是S的一个子集,差集是两个集合之间的差。 代码实现// 交集:
let InterSection = function (arr1, arr2) {
return arr1.filter((v) => arr2.indexOf(v) > -1);
}
// 并集:
let Union = function (arr1, arr2) {
return arr1.concat(arr2.filter((v) => !(arr1.indexOf(v) > -1)));
}
// 补集:
let Complement = function (arr1, arr2) {
let all
return arr1.filter((v) => !(arr2.indexOf(v) > -1));
}
// 差集:
let DifferenceSet = function (arr1, arr2) {
return arr1.filter((v) => arr2.indexOf(v) == -1);
}
// console.log(InterSection([1,2,3,4,5], [3,4,6,7]));
// console.log(Union([1,2,3,4,5], [3,4,6,7]));
// console.log(Complement([1,2,3,4,5], [3,4,6,7]));
// console.log(DifferenceSet([1,2,3,4,5], [3,4,6,7])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: