Several methods of array de-weight

Source: Internet
Author: User

  //First: The simplest array de-/** a new array, iterating through the incoming array, the value is not in the new array to push into the new array * IE8 The following indexof methods that do not support arrays * */function Uniq (array) {    var temp = []; A new temporary array     for (var i = 0; i < array.length; i++) {        if (Temp.indexof (array[i]) = = -1) {            Temp.push (Array[i]);       }   }  &nbsp ; return temp;} var AA = [1,2,2,4,9,6,7,5,2,3,5,6,5];console.log (Uniq (AA));//Second de-weight:/** is the fastest, occupying the most space (space change time) * * The method executes faster than any other method, is to occupy a bit more memory. * Current ideas: Create a new JS object and the newly array, when traversing the incoming array, determine whether the value is the key of the JS object, * Not to add the key to the object and put in the new array. * Note: When judging whether the JS object key, the incoming key will be automatically executed "toString ()", * different keys may be mistaken, such as n[val]--n[1], n["1"];* solve the above problem or call "IndexOf". */function uniq (array) {    var temp = {}, R = [], Len = Array.Length, Val, type;    for (var i = 0; i < Len; i++) {        val = array[i];        type = typeof val;        if (!temp[val]) {            TEMP[val] = [type];            R.push (val);       } else if (Temp[val].inde XOf (Type) < 0) {            Temp[val].push (type);          &NBSP ; R.push (val);       }   }    return R; var AA = [2], 4,9, "a", "a", 2,3,5,6,5];console.log (Uniq (AA));//The Third de-/** to sort the incoming array, sort the same values next to each other, and then iterate, the new array only adds values that do not duplicate the previous value. * Will disrupt the order of the original array * */function Uniq (array) {    Array.Sort ();    var temp=[array[0]];    for (var i = 1; i < Array.Length; i++) {        if (Array[i]!== temp[temp.length-1]) {            Temp.push (Array[i]);       }   }    return temp;} var AA = [2], 4,9, "a", "a", 2,3,5,6,5];console.log (Uniq (AA));//fourth to/*** or to call "IndexOf" performance is similar to Method 1, * Implementation idea: If the current array of item I is in the current array where the first occurrence is not i,* then the term I is duplicated, ignored. Otherwise, the result array is deposited. * */function Uniq (array) {    var teMP = [];    for (var i = 0; i < array.length; i++) {       //If the item I of the current array first appears in the current array is I, only The array is stored, otherwise the representation is duplicated         if (array.indexof (array[i) = = i) {            TEMP.P Ush (Array[i])        }   }    return temp;} var AA = [2], 4,9, "a", "a", 2,3,5,6,5];console.log (Uniq (AA));//The Fifth deduplication//idea: Get the right-most value without repeating to put in the new array/** recommended method * * The implementation code of the method is pretty cool, * Implementation idea: Gets the right-most value that is not duplicated into the new array. * (the next round of judgment that terminates the current loop while a duplicate value is detected and enters the top loop at the same time) */function Uniq (array) {    var temp = [];    var L = Array.length;&nbs P   for (var i = 0; i < L, i++) {        for (var j = i + 1; j < L; j + +) {      & nbsp     if (array[i] = = = Array[j]) {                i++;                j = i;           }       }  &N Bsp     Temp.push (array[i]);   }    return temp;} var AA = [1,2,2,3,5,3,6,5];console.log (Uniq (AA)); 

Several ways to de-weight an array

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.