#90 判断两个 Set 是否相同


  • 0
    administrators

    完成 isSameSet 函数,它接受了两个 Set 对象作为参数,请你返回 true/false 来表明这两个 set 的内容是否完全一致,例如:

    const a = {}
    const b = 1
    const c = 'ScriptOJ'
    
    const set1 = new Set([a, b, c])
    const set2 = new Set([a, c, b])
    
    isSameSet(set1, set2) // => true
    

  • 0
    管理员

    const isSameSet = (set1, set2) => [...set1].every((o) => set2.has(o)) && [...set2].every((o) => set1.has(o))
    

  • 1

    const isSameSet = (s1, s2) => {
      if (s1.size !== s2.size) {
        return false;
      }
      return [...s1].every(i => s2.has(i))
    }
    

  • 2

    const isSameSet = (set1, set2) => {
        let s = new Set([...set1, ...set2])
        return s.size == set1.size && s.size == set2.size
    }

  • 0

    const isSameSet = (s1, s2) => s1.size === s2.size && [...s1].filter(x => s2.has(x)).length === s1.size;
    

  • 0

    const isSameSet = (s1, s2) => {
    for(let item of s1){
    if(s2.has(item)){
    s2.delete(item);
    }else{
    return false;
    }
    }
    if(s2.size == 0){
    return true;
    }else{
    return false;
    }
    }


登录后回复
 

与 ScriptOJ 的连接断开,我们正在尝试重连,请耐心等待