0
var a = [
  [
    {
      id: "AAA"
    },
    {
      id: "BBB"
    }
  ],
  [
    {
      id: "AAA"
    },
    {
      id: "DDD"
    }
  ]
];
var b = [
  [
    {
      id: "BBB"
    }
  ],
  [
    {
      id: "CCC"
    },
    {
      id: "BBB"
    }
  ],
  [
    {
      id: "AAA"
    }
  ],
  [
    {
      id: "AAA"
    },
    {
      id: "DDD"
    }
  ],
  [
    {
      id: "DDD"
    }
  ],
  [
    {
      id: "CCC"
    },
    {
      id: "DDD"
    }
  ],
  [
    {
      id: "AAA"
    }
  ],
  [
    {
      id: "AAA"
    },
    {
      id: "BBB"
    }
  ]
];

function remove_duplicates(a, b) {
    for (var i = 0, len = a.length; i < len; i++) {
        for (var j = 0, len = b.length; j < len; j++) {
            if (a[i].name == b[j].name) {
                b.splice(j, 1);
            }
        }
    }

    console.log(a);
    console.log(b);

}

console.log(a);
console.log(b);

remove_duplicates(a,b);

I have tried the filter and reduce but they are coming with desired result I found a similar one but the one I have got little different structure

seeking for possible solution from JavaScript or underscore

expected result: [[{"id":"BBB"}],[{"id":"CCC"},{"id":"BBB"}],[{"id":"AAA"}],[‌​{"id":"DDD"}],[{"id"‌​:"CCC"},{"id":"DDD"}‌​]]

Community
  • 1
  • 1
Sri
  • 49
  • 1
  • 7

1 Answers1

0

First we need to know when two arrays are equal. I have defined this in the isEqual function such that an array is equal to another array if they are the same length and the objects have the same id in the same order.

Next, we need to understand what it means to 'remove duplicates'. Imagine if you have two arrays of numbers. [1,2,3,4] and [2,4,5,6]. We'll expect the result to be [1,3,5,6]. We do this by getting the first array and subtracting any items that exist in the second array and then getting the second array and subtracting any items that exist in the first array. Finally we join the two results together. This is what is happening in the remove_duplicates function.

var a = [
  [
    {
      id: "AAA"
    },
    {
      id: "BBB"
    }
  ],
  [
    {
      id: "AAA"
    },
    {
      id: "DDD"
    }
  ]
];
var b = [
  [
    {
      id: "BBB"
    }
  ],
  [
    {
      id: "CCC"
    },
    {
      id: "BBB"
    }
  ],
  [
    {
      id: "AAA"
    }
  ],
  [
    {
      id: "AAA"
    },
    {
      id: "DDD"
    }
  ],
  [
    {
      id: "DDD"
    }
  ],
  [
    {
      id: "CCC"
    },
    {
      id: "DDD"
    }
  ],
  [
    {
      id: "AAA"
    }
  ],
  [
    {
      id: "AAA"
    },
    {
      id: "BBB"
    }
  ]
];

function remove_duplicates(a, b) {
  var setOfB = setOf(b);
  var setOfA = setOf(a);

  var aMinusB = minus(setOfA, setOfB);
  var bMinusA = minus(setOfB, setOfA);
  return aMinusB.concat(bMinusA);
}

function setOf(array){
  return _.uniq(array, false, JSON.stringify);
}

function minus(a,b) {
  return _.reject(b, function(item){
    return _.find(a, _.partial(_.isEqual, item));
  });
}


// var expected = [
//   [{"id":"BBB"}],
//   [{"id":"CCC"},{"id":"BBB"}],
//   [{"id":"AAA"}],
//   [{"id":"DDD"}],
//   [{"id":"CCC"},{"id":"DDD"}]
// ];


console.log(remove_duplicates(a,b));
derp
  • 2,300
  • 13
  • 20
  • thank you appreciate for very quick response, but this is still duplicating after executing i see AAA got repeated. Result for given code: `[[{"id":"BBB"}],[{"id":"CCC"},{"id":"BBB"}],[{"id":"AAA"}],[{"id":"DDD"}],[{"id":"CCC"},{"id":"DDD"}],[{"id":"AAA"}]]` and the expected result: `[[{"id":"BBB"}],[{"id":"CCC"},{"id":"BBB"}],[{"id":"AAA"}],[{"id":"DDD"}],[{"id":"CCC"},{"id":"DDD"}]]` – Sri Jan 17 '17 at 06:51
  • can we avoid adding duplicates or remove duplicates at the end from the object that is returned – Sri Jan 17 '17 at 07:47
  • can you please explain what is the use of `var bMinusA = minus(b,a);` when i checked the length it is returning 0 – Sri Jan 17 '17 at 09:34
  • ah yes, i forgot to remove the duplicates within B. Solution has been updated – derp Jan 17 '17 at 23:32
  • the use of bMinusA is to remove the items in A from the items in B. Although they do not exist in your problem, they may exist if you pass in a different array into the remove_duplicates function. Read up on the following wiki page: https://en.wikipedia.org/wiki/Exclusive_or – derp Jan 17 '17 at 23:35