JavaScript 如何获取两个数组之间的对称差异

JavaScript 如何获取两个数组之间的对称差异

在本文中,我们将看到如何使用JavaScript获取两个数组之间的对称差异。

在数学中,两个集合A和B之间的对称差异被表示为A Δ B = (A-B) ∪ (B-A)

  • 它被定义为同时存在于集合A或集合B中但不同时存在于两者中的所有元素的集合。
  • 简单来说,公共元素从两个集合中丢弃。

例如:

A = { 1, 2, 3, 4, 5, 6}
B = { 4, 5, 6, 7 }
A - B = { 1, 2, 3, 4, 5, 6} - { 4, 5, 6, 7 }
      = { 1, 2, 3 }
B - A = { 4, 5, 6, 7 } - { 1, 2, 3, 4, 5, 6}
      = { 7, 1, 2, 3 }

A Δ B = ( A - B ) ∪ ( B - A )
      = { 1, 2, 3 } ∪ { 7, 1, 2, 3 }
A Δ B = { 1, 2, 3, 7 }

我们可以通过以下方法实现这一点:

获取对称差集的方法

  • 幼稚的方法 – 使用Javascript的for循环。
  • 使用filter()和includes()方法。
  • 使用Set和filter()方法。
  • 使用reduce()和includes()方法。

方法1: 幼稚的方法 – 使用Javascript的for循环

示例: 在这个示例中,我们将使用Javascript的for循环来找到两个数组的对称差集。

// Defining two arrays and a resultant array
const a = [1, 2, 3, 4, 5, 7, 9];
const b = [5, 6, 7, 8, 9];
const result = [];
 
// Defining the function with two
// arguments array inputs
function difference(arr1, arr2) {
    let i = 0,
        j = 0;
    let flag = false;
 
    /* For array 1 */
    for (i = 0; i < arr1.length; i++) {
        // Resetting the flag and the
        // other array iterator
        j = 0;
        flag = false;
        while (j != arr2.length) {
            if (arr1[i] == arr2[j]) {
                flag = true;
                break;
            }
            j++;
        }
 
        /* If value is not present in the 
            second array then push that value 
            to the resultant array */
        if (!flag) {
            result.push(arr1[i]);
        }
    }
    flag = false;
 
    // For array 2
    for (i = 0; i < arr2.length; i++) {
        // Resetting the flag and the
        // other array iterator
        j = 0;
        flag = false;
        while (j != arr1.length) {
            if (arr2[i] == arr1[j]) {
                flag = true;
                break;
            }
            j++;
        }
 
        /* If value is not present in the
            first array then push that value 
            to the resultant array */
        if (!flag) {
            result.push(arr2[i]);
        }
    }
    return result;
}
console.log(difference(a, b));

输出

[ 1, 2, 3, 4, 6, 8 ]

方法2:使用filter()方法和includes()方法

在这种方法中,我们使用filter()方法对第一个数组进行过滤,并使用includes()方法检查每个项是否不在第二个数组中。产生的数组包含在第一个数组中存在但不在第二个数组中的元素。

语法:

array1.filter((element) => !array2.includes(element));

示例: 在此示例中,我们使用 filter()includes() 方法来获取数组之间的差异,如下所示:

// Defining two arrays and a resultant array
const array1 = [1, 2, 3, 4, 5, 7, 9];
const array2 = [5, 6, 7, 8, 9];
 
// Getting elements in array1 but not array2
const difference1 = array1.filter(
  (element) => !array2.includes(element));
 
// Getting elements in array2 but not array1
const difference2 = array2.filter(
  (element) => !array1.includes(element));
 
// Combining the differences
const result = [...difference1, ...difference2];
 
// Showing the output
console.log(result);

输出

[ 1, 2, 3, 4, 6, 8 ]

方法3: 使用Set和filter()方法

在这种方法中,数组通过Set构造函数转换为Set。Set数据结构只允许唯一的值。通过过滤掉第一个Set中也存在于第二个Set中的元素,我们可以得到差异。

语法:

const difference = [...set1].filter((element) => !set2.has(element));

示例: 在这个示例中,我们将使用set和filter()方法获取数组的差异,并将组合结果作为输出显示:

// Defining two arrays
const array1 = [1, 2, 3, 4, 5, 7, 9];
const array2 = [5, 6, 7, 8, 9];
 
// Converting arrrays to sets using set method
const set1 = new Set(array1);
const set2 = new Set(array2);
 
// Geting the differences using filter method
const difference1 = [...set1].filter((element) => !set2.has(element));
const difference2 = [...set2].filter((element) => !set1.has(element));
 
// Combining differnce arrays
const result = [...difference1, ...difference2];
 
// Showing the result as output
console.log(result);

输出

[ 1, 2, 3, 4, 6, 8 ]

方法4:使用reduce()和includes()方法

该方法遍历第一个数组,并使用includes()方法检查每个元素是否存在于第二个数组中。它累积在第二个数组中找不到的元素,从而得到两个数组之间的差异。

语法:

const difference = array1.reduce((result, element) => {
    if (array2.indexOf(element) === -1) {
        result.push(element);
    }
    return result;
}, []);

示例: 在这个示例中,我们将使用reduce()和includes()方法获取数组的差异,并将组合的结果作为输出展示:

// Defining two arrays
const array1 = [1, 2, 3, 4, 5, 7, 9];
const array2 = [5, 6, 7, 8, 9];
 
// Getting elemment in array1 but not in array2
const difference1 = array1.reduce((result, element) => {
    if (array2.indexOf(element) === -1) {
        result.push(element);
    }
    return result;
}, []);
 
// Getting elemements in array2 but not in array1
const difference2 = array2.reduce((result, element) => {
    if (array1.indexOf(element) === -1) {
        result.push(element);
    }
    return result;
}, []);
 
// Combining the diffeerence using spread operator
const result = [...difference1, ...difference2];
 
// Show the result as output
console.log(result);

输出

[ 1, 2, 3, 4, 6, 8 ]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程