19、求两个数组的交集

let num1 = [1,4,2,6,9]
let num2 = [3,4,7,2]

//方法一
let arr = []
for(let i = 0;i < num1.length;i++){
  let item1 = num1[i]
  for(let j = 0;j<num2.length;j++){
    let item2 = num2[j]
    if(item1 === item2){
      arr.push(item1)
      num1[i] = null;
      num2[j] = null;
      break;
    }
  }
}

//方法二
let arr = []
num1.forEach((item,index)=>{
    let n = arr2.indexOf(item)
    if(n>=0){
    arr.push(item)
    num1.splice(index,1)
    num2.splice(n,1)
    }
})

results matching ""

    No results matching ""