FCC Intermediate algorithm problem comparison two arrays

Source: Internet
Author: User
Tags diff

Diff. Arrays

Compares two arrays and returns a new array whose elements are two of all unique array elements in a given array. In other words, returns the difference of two arrays.

Comparisonoperators

Array.slice ()

Array.filter ()

Array.indexof ()

Array.concat ()

Ideas:

First find the arr1 in the different parts of the arr2 to form a new array 1, and then find the arr2 in the different parts of the arr1 form a new array 2, the two arrays are merged to produce two different parts of the array.

Knowledge Points:

(1) var newarr=arr.filter (func);

The filter function can filter out elements that allow the Func function to return true and form a new array, and the Func function defaults to a single element in the value array;

1  var newArr1 = arr1.filter (COMP1); 2  var

(2) Arr.indexof (value) ===-1;

The IndexOf function can be found on an array, and the number returned is the index of the value in arr if it can find value in arr.

1  function Comp1 (value) {2         if (Arr2.indexof (value) = = =-1) {3             return true ; 4         }5     }

(3) NewArr = Newarr1.concat (NEWARR2);

The CONCAT function can implement a combination of two arrays, newArr1 and newArr2 to form a new array.

1 newArr = Newarr1.concat (NEWARR2);

Code:

1 functiondiff (Arr1, arr2) {2     varNEWARR = [];3 4     functionComp1 (value) {5         if(Arr2.indexof (value) = = = 1) {6             return true;7         }8     }9 Ten     functionCOMP2 (value) { One         if(Arr1.indexof (value) = = = 1) { A             return true; -         } -     } the     varNEWARR1 =Arr1.filter (COMP1); -     varNEWARR2 =Arr2.filter (COMP2); -NEWARR =Newarr1.concat (NEWARR2); -     returnNEWARR; + } - diff ( +["Andesite", "Grass", "Dirt", "pink wool", "dead Shrub"], ["Diorite", "andesite", "Grass", "Dirt", "dead Shrub"]);

FCC Intermediate algorithm problem comparison two arrays

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.