加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
39-数组并集,交集,差集.html 1.34 KB
一键复制 编辑 原始数据 按行查看 历史
wangcai 提交于 2023-04-04 16:49 . xx‘
<!-- <script>
// 并集
// [1,2,3] 并 [4,5,6] ==> [1,2,3,4,5,6]
// [1,2,3,3] 并 [4,5,6,6] ==> [1,2,3,4,5,6]
// [1,2,3,3] 并 [3,6,6] ==> [1,2,3,6]
let arr1 = [1,2,3,3];
let arr2 = [4,5,6,3,2,4,9];
function union(arr1,arr2){
let res = new Set([...arr1,...arr2])
return [...res]
}
let newArr = union(arr1,arr2);
console.log(newArr)
</script> -->
<!-- <script>
// 交集
// [1,2,3] 交 [3,4,5,6] ==> [3]
// [1,2,3] 交 [7,8,9] ==> []
// [1,2,3] 交 [1,2,3] ==> [1,2,3]
let arr1 = [1,2,3,3];
let arr2 = [3,4,5,5];
function intersection(arr1,arr2){
let s1 = new Set(arr1)
let s2 = new Set(arr2)
let newArr = [...s1].filter(item=>{
return s2.has(item)
})
return newArr
}
let newArr = intersection(arr1,arr2);
console.log(newArr)
</script> -->
<script>
// 差集
// [1,2,3] 差 [3,4,5] ==> [1,2]
// [3,4,5] 差 [1,2,3] ==> [4,5]
let arr1 = [1, 2, 3];
let arr2 = [3, 4, 5];
function except(arr1, arr2) {
let s1 = new Set(arr1)
let s2 = new Set(arr2)
let newArr = [...s1].filter(item => {
return !s2.has(item)
})
return newArr;
}
console.log(except(arr1, arr2)) // [1,2]
console.log(except(arr2, arr1)) // [4,5]
</script>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化