JS Array de-weight

Source: Internet
Author: User

JS Array to go heavy is also the cliché of the topic, how to drive the insect nirvana, the most easy to think of is to create a new array directly, storing the array after the heavy. Because Nan this magical figure even he does not know himself, so the target array if need to go heavy Nan, need to use isNaN to judge, OK, code to the true, the code is as follows:

function Unique01 (arr) {if(Array.isarray (arr)) {varresult = []; varFlag =true;  for(varI=0; i<arr.length;i++){            if(Result.indexof (Arr[i]) ===-1){                if(IsNaN (Arr[i]) &&flag)                    {Result.push (arr[i]); Flag=false; }                Else if(IsNaN (Arr[i])) {Continue; }                Else{Result.push (arr[i]); }            }        }        returnresult; }    Else{        return false; }}

Although this method is not very difficult to achieve, the shortcomings are really obvious, one is not fast enough, time complexity O (n) 2. The second is the fee space, need to open an array to store the results.

How can not cost space, directly in the original array operation, and finally return this array, ah, the idea has, see how to achieve, continue to put code.

functionUnique02 (arr) {if(Array.isarray (arr)) {varFlag =true;  for(vari=arr.length-1;i>0;i--){            if(IsNaN (Arr[i])) {flag=false; }             for(varj=0;j<i;j++){                if(IsNaN (Arr[j]) && (flag===false) {Arr.splice (i,1); }                Else if(arr[j]===Arr[i]) {Arr.splice (i,1); }            }        }        returnarr; }    Else{        return false; }}

Look carefully, this operation is still a bit level, although the time complexity is still O (n) 2, at least the space is not wasted, of course, our goal will not stop here

functionunique03 (arr) {if(Array.isarray (arr)) {varresult=[],obj={},val,type;  for(vari=0;i<arr.length;i++) {Val=Arr[i]; Type=typeofArr[i]; if(!Obj[val]) {Obj[val]=[Type];            Result.push (Val); }            Else if(Obj[val].indexof (type) ==-1) {Obj[val].push (type);            Result.push (Val); }        }        returnresult; }Else{        return false; }}

is not more than 6, yes, this time we have reduced the complexity of the O (n), but a bit of regret is a bit more space wasted, with the cost of space to exchange time. Whether it's worth it or not, you decide.

Of course, there is no 6, only the more 6,ES6 code actually writes like this

function unique04 (arr) {    return array.from (new  Set (arr))}

Yes, you have not read wrong, only one line, a line of code, to do everything, is this feel.

JS Array de-weight

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.