欢迎来到码农权的博客 MaNongGeGe.|

两个数组的并集、交集、差集

    // 两个数组的并集、交集、差集
    // 不能出现重复项,得到的结果是一个新的数组
    const arr1 = [33, 22, 55, 33, 11, 33, 5];
    const arr2 = [22, 55, 77, 88, 88, 99, 99];

    // 并集
    const union = [...new Set([...arr1, ...arr2])];
    console.log(`并集 => ${union}`);

    // 交集
    const cross = [...new Set(arr1)].filter(item => {
      return arr2.includes(item)
    })
    console.log(`交集 => ${cross}`);

    // 差集
    const diff = union.filter(item => {
      return !cross.includes(item)
    })
    console.log(`差集 => ${diff}`);

本文作者:HuangBingQuan

本文链接:https://www.cnblogs.com/bingquan1/p/17001939.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   HuangBingQuan  阅读(45)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 不将就 码农权
  2. 2 Bleeding Love 码农权
  3. 3 想你念你 码农权
Bleeding Love - 码农权
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

Not available