每天一道算法题(2022-11-13)

58 阅读1分钟

算出两个数组的补集,数组只包含字符串和数字

(如果 b 是 a 的子集,返回存在于 a 不存在于 b 的元素集合,反之返回空集合)

function findComplementarySet(a: any[], b: any[]): any[] {
  const x = [...a.filter(x=>!b.includes(x))]  
  const y = [...b.filter(x=>!a.includes(x))]  
  const list = [...x,...y];  
  return y.length?[]:list.sort()
}