Creates a function that accepts two or more arrays and returns an array of equivalent differential (symmetric difference) (or) of the given array △
⊕
.
Gives two sets, such as a collectionA = {1, 2, 3}
and collectionsB = {2, 3, 4}
), and the mathematical term "equivalent differential" is a collection of all elements that are only one of the two sets (A △ B = C = {1, 4}
). For incoming additional collections, such asD = {2, 3}
), You should install the preceding principle to seek the result of the first two sets with the peer differential set of the new set (C △ D = {1, 4} △ {2, 3} = {1, 2, 3, 4}
).
The FCC advanced algorithm is a relatively simple one, my method:
1 functionsym (args) {2 //the function is to ensure that the number in each array is not duplicated, repeating only one3 functionnorepeat (arr) {4 returnArr.filter (function(val,index,array) {5 returnArray.indexof (val) = = =index;6 });7 }8 //If there are repeated numbers after concat, remove all the repetitions .9 functionAdd (arr1,arr2) {Ten varArr=norepeat (arr1). concat (norepeat (arr2)); one returnArr.filter (function(val,index,array) { a returnArray.indexof (val) = = =Array.lastindexof (val); - }); - } the //building a parameter array - varArr=[]; - for(vari=0;i<arguments.length;i++){ - Arr.push (arguments[i]); + } - //accumulate and sort + returnArr.reduce (add). Sort (function(num1,num2) { a returnNum1-num2; at }); - } - -Sym ([3, 3, 3, 2, 5], [2, 1, 5, 17], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]);
Remove the number of repetitions using the filter, indexof, and LastIndexOf methods, you are welcome to Criticize.
JS-FCC algorithm-symmetric Difference