#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))
    

  • 2

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

  • 5

    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;
    }
    }


  • 0

    const isSameSet = (s1, s2) => {
      function isContain(aSet, bSet){
        return ![...aSet].some(item => !bSet.has(item))
      }
      return isContain(s1, s2) && isContain(s2, s1);
    }
    

  • 0

    const isSameSet = (s1, s2) => {
    const getComp = v=>JSON.stringify([...v].sort());
    return getComp(s1) === getComp(s2)
    }


  • 0

    @陈小俊 此处的has用法是哪个对象的方法?


  • 0

    @陈小俊 我看懂了~


  • 0

    const isSameSet = (s1, s2) => {
    let diff1 = new Set([...s1].filter(x => !s2.has(x)))
    let diff2 = new Set([...s2].filter(x => !s1.has(x)))
    if(diff1.size || diff2.size) {
    return false
    }
    return true
    }


  • 0

    写的不好,但我能想到的就是这样了

    const isSameSet = (s1, s2) => {
    
       function fn(...rest) {
           let arr = [];
           for (let i = 0; i < rest.length; i++) {
               for (let k of rest[i]) {
                   arr.push(k);
               }
           }
           return new Set(arr);
       }
    
       if (s1.size === s2.size) {
           if (fn(s1, s2).size === s1.size) {
               return true;
           }
           return false;
       }
       return false;
    };

  • 0

    @Reson_a 这个方法使用了set的不重复属性,如果两个set相等,合并后和合并前的size一定相等


  • 0

    		const isSameSet = (s1, s2) => {/* TODO */
    
    			let arr1 = [...s1] , arr2 = [...s2];
    
    			let a = arr1.every(item => arr2.includes(item))
    			let b = arr2.every(item => arr1.includes(item))
    
    			return a && b && arr1.length == arr1.length
    	}
    

  • 0

    const isSameSet = (s1, s2) => /* TODO */{
    var resArr = new Set([]);
    for(let item of s1){
    resArr.add(s2.has(item))
    }
    return !resArr.has(false)
    }
    为什么这样不可以过?


  • 0

    const isSameSet = (s1, s2) => {
    if(s1.size !== s2.size){return false}
    let arr1 = [...s1];
    let arr2 = [...s2];
    let set = new Set([...arr1, ...arr2]);
    let arr3 = Array.from(set);
    return arr3.length === arr2.length
    }


登录后回复
 

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