简体   繁体   English

找到两个数组之间的对称差异

[英]Find symmetric difference between two arrays

I'd like to find the symmetric difference between TWO arrays.我想找到两个数组之间的对称差异。 This implementation works, however I'd like to write a function that is specific to only two arrays, rather than one that finds the symmetric difference between a bunch of arrays. 这个实现有效,但是我想编写一个只针对两个数组的函数,而不是一个找到一堆数组之间对称差异的函数。 The function should like this:该函数应该是这样的:

function diffArray(arr1, arr2) { }

then return a new array with the symmetric difference.然后返回一个具有对称差异的新数组。

My best attempt so far was到目前为止我最好的尝试是

var newArr = [];
    for (var i = 0; i < arr1.length; i++){
      var x = arr[i]; 
    for (var n = 0; n < arr2.length; n++){
      var y = arr2[n];
      if (y === x){
        break;
       } else {
      newArr.push(y);
    }
  }
 }

However, I know this is not even close.但是,我知道这还不是很接近。 The question (it is an algorithm problem for FreeCodeCamp) hints to use the methods array.filter(), array.indexOf(), array.concat(), array.slice() in the implementation.该问题(它是 FreeCodeCamp 的算法问题)提示在实现中使用方法 array.filter()、array.indexOf()、array.concat()、array.slice()。 I understand that the general idea is to take each element in one of the arrays (the first one in my case), then compare it to every element in the second array.我知道一般的想法是获取其中一个数组中的每个元素(在我的例子中是第一个),然后将它与第二个数组中的每个元素进行比较。 If no matches are found, push that element into the newArr.如果未找到匹配项,则将该元素推送到 newArr 中。

Can anyone help with a sound implementation that uses the aforementioned methods and provide a solid explanation/comments on how it works?任何人都可以帮助使用上述方法的合理实现并提供有关其工作原理的可靠解释/评论吗?

Thank you!谢谢!

Here is another idea:这是另一个想法:

function diffArray(arr1, arr2) {
    var newArr = [];

    return arr1.filter(function(val) {
        return arr2.indexOf(val) === -1;
    })
    /*the method above, returns a new array, so you can chain it
          to concat with the array returned from the filter() method
          in the arr2...*/

        .concat(arr2.filter(function(val) {
            return arr1.indexOf(val) === -1;
        }));
}

This is my simple solution这是我的简单解决方案

function diffArray(a, b){
c = a.concat(b)
d = [];
var diffarr = c.filter(function(c1){
   if (a.indexOf(c1) === -1 || b.indexOf(c1) === -1){
        d.push(c1)
   }
})
return d;}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]); diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

Alright.好吧。 I solved it, but I think it can still be done better.我解决了它,但我认为它仍然可以做得更好。

function diffArray(arr1, arr2) {
  var newArray = [];

  function inArray2(value){
      if(arr2.indexOf(value) == -1){
        return true;
      }
    return false;
  }

  function inArray1(value){
    if(arr1.indexOf(value) == -1){
      return true;
    }
    return false;
  }

  var arr1Filtered = arr1.filter(inArray2);
  var arr2Filtered = arr2.filter(inArray1);

  newArray = arr1Filtered.concat(arr2Filtered);
  return newArray;
}

Since this passed all the test cases, I assume it is correct for all cases.由于这通过了所有测试用例,我认为它对所有用例都是正确的。 Whew.哇。

UPDATE: New and improved algorithm, thanks to helpful input from torazaburo .更新:新的和改进的算法,感谢torazaburo 的有用输入。 Hope this helps anyone who is also stuck on this challenge.希望这可以帮助任何也陷入这一挑战的人。

function diffArray(arr1, arr2) {
   var newArray = [];

   function notInArray2(value){
       return arr2.indexOf(value) === -1;
   }
   function notInArray1(value){
     return arr1.indexOf(value) === -1;
   }

   var arr1Filtered = arr1.filter(notInArray2);
   var arr2Filtered = arr2.filter(notInArray1);

   newArray = arr1Filtered.concat(arr2Filtered);
   return newArray;
}

This can be solved by combining multiple array methods.这可以通过组合多种数组方法来解决。

Below solution uses concat, reduce and includes.下面的解决方案使用 concat、reduce 和 includes。

function arrayDifference(arr1, arr2) {
  return arr1
    .concat(arr2)
    .filter(item => !arr1.includes(item) || !arr2.includes(item));
}

console.log(arrayDifference([1, 2, 5], [1, 2, 3, 4, 5, 4])) // [3, 4, 4]
console.log(arrayDifference([1, 2, 5, 4], [1, 2, 3, 4]))    // [5, 3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM