With ES6 clever solution to the traditional interview algorithm small problem!

Source: Internet
Author: User

Recently, I was also preparing for the interview, in the review algorithm, witty use of a wave of ES6. Come and see!

1. The de-weight of the array

        var arr=str.split (');          for (var i=0;i<arr.length-1;i++) {            for (var j=i+1;j<arr.length;j++) {                if(arr[i]==arr[j]) {                    Arr.splice (J,1);     //                 arr.length-=1; Delete the length automatically minus 1                    j--;                    Console.log (arr)         }}}

It seems a bit troublesome to do it with two layers of loops. It seems that the set in ES6 can be used. Try?

    var arr=[1,3,55,12,32,3,66,12,358,66,73,32];     var a=New Set (arr); // set is a collection of class arrays     Console.log (a,a.size); //     Arr=array.from (a); Converts an array of classes to array    arr=[...a];   Extension Operators

What? So easy, not so much logic, and very little code. It's so cool!!!

2. How to splice two arrays?

The traditional for loop does not say much, of course concat is also possible, or directly on the ES6 bar!

    var = [arr1];     var arr2 = [4,5,6];    Arr1.push (... arr2);

or something like that.

   var = [arr1];    var arr2 = [4,5,6];    var arr3 = [... arr1,... arr2];

Later, you will continue to write questions about the algorithm, or ES6. Want to feel good small partners, can order a praise add a concern about what.

(When writing the first blog, the content is too much, too miscellaneous, will walk the text less, concise style, after all, I look at others blog when, content too much or miscellaneous words, it is easy to head pain).

PS: Never copy other people's blogs, pure their own hands to fight.

With ES6 clever solution to the traditional interview algorithm small problem!

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.