The common Subarray is drawn from the multiple arrays of reduce, and the number of occurrences of the array elements is counted.

Source: Internet
Author: User

Yesterday did a group of face test, the request is to n array, find the n number of common sub-arrays.

varA = [7,2,3,4,5];varb = [4,2,3,7,6];varc = [2,3,3,3,7];varD = [4,2,3,8,7];

Above four arrays, with common sub-array 2, 3,7

function Main () {    var result = [];     var arr = arguments[0];      for (var i=1 ; i<arguments.length; i++) {        var arr = arguments[i]. Reduce ((a,item) ={            arr.indexof (item)!=-1 && a.push (item)             return a        },[])    }     return arr}

Concrete methods such as above, ES6 fresh and refined wording, with good reduce can save a lot of things.

The same can be done when the number of elements of an array of the above array should occur;

function Main () {    var arr = [].reduce.call (arguments, (a,item) =a.concat (item), []);     return arr.reduce (a,num) ={        && ++a[num] | | (a[num]=1);          return a    },{})}

This reduce has no use of fresh popular AH.

I know everyone in the heart must think of a good fresh force ah.

Yes, learn to reduce the force, other people only shout 666 of the share.

Come on!!!

The common Subarray is drawn from the multiple arrays of reduce, and the number of occurrences of the array elements is counted.

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.