JS-FCC algorithm-symmetric Difference

Source: Internet
Author: User
Tags new set

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.