innerWang / blogs

1 stars 0 forks source link

各类算法题 #20

Open innerWang opened 5 years ago

innerWang commented 5 years ago

主要是记录各类算法题。

innerWang commented 5 years ago

1:两个无序有重复元素的数组,如何判断B是A的子集

const arr1 = [7,9,3,4,2,6,3,1,2]
const arr2 = [3,4,9,5,2]

const subset = (A,B)=>{
  var tempArr = A
  for(let i=0;i<B.length;i++){
    if(tempArr.indexOf(B[i]) === -1){
      return false
    }else{
      tempArr.splice(tempArr.indexOf(B[i]),1)
    }
  }
  return true
}

console.log(subset(arr1,arr2))