Several methods for de-duplicating Javascript arrays

Source: Internet
Author: User
Implementation idea: Create a js object and a new array. when traversing the input array, determine whether the value is the key of the js object. if not, add the key to the object and add it to the new array. NOTE: & amp; nbsp; when determining whether it is a js object key, the system automatically runs "toString ()" on the input key ()

Implementation idea: create a new array and traverse the input array. if the value is not in the new array, add it to the new array. note: The ECMAScript5 method is used to determine whether the value is in the array, internet Explorer 8 is not supported. you need to write more compatible low-version browser code:

// Simplest array deduplication
Function unique1 (array ){
Var n = []; // A new temporary array
// Traverse the current array
For (var I = 0; I <array. length; I ++ ){
// If the I of the current array has been saved as a temporary array, skip this step,
// Otherwise, push the current item to the temporary array.
If (n. indexOf (array [I]) =-1) n. push (array [I]);
}
Return n;
}

Object key-value pair method

Implementation idea: Create a js object and a new array. when traversing the input array, determine whether the value is the key of the js object. if not, add the key to the object and add it to the new array. Note: When determining whether it is a js object key, "toString ()" is automatically executed on the input key. different keys may be mistakenly considered the same. for example: a [1], a ["1"]. To solve the above problem, you must call "indexOf ".

// The fastest speed, the most space occupied (space for time)
Function unique2 (array ){
Var n = {}, r = [], len = array. length, val, type;
For (var I = 0; I <array. length; I ++ ){
Val = array [I];
Type = typeof val;
If (! N [val]) {
N [val] = [type];
R. push (val );
} Else if (n [val]. indexOf (type) <0 ){
N [val]. push (type );
R. push (val );
}
}
Return r;
}

Array subscript limit method

Implementation idea: if the position where the I entry of the current array appears for the first time in the current array is not I, it indicates that the I entry is repeated and ignored. Otherwise, it is saved to the result array.

Function unique3 (array ){
Var n = [array [0]; // result array
// Traverse from the second entry
For (var I = 1; I <array. length; I ++ ){
// If the position where the I entry of the current array appears for the first time in the current array is not I,
// This indicates that the I-th item is repeated and ignored. Otherwise, the result array is saved.
If (array. indexOf (array [I]) = I) n. push (array [I]);
}
Return n;
}

Adjacent removal method after sorting

Implementation idea: sort the input array, and the same value is adjacent after sorting. Then, when traversing, the new array only adds values that are not repeated with the previous value.

// Adjacent the same value, and traverse to remove duplicate values
Function unique4 (array ){
Array. sort ();
Var re = [array [0];
For (var I = 1; I <array. length; I ++ ){
If (array [I]! = Re [re. length-1])
{
Re. push (array [I]);
}
}
Return re;
}

Optimize the traversal array method

Implementation idea: get the rightmost value that is not repeated and put it into the new array. (If a duplicate value is detected, terminate the current loop and enter the next round of judgment in the top-level loop)

// Train of thought: Get the rightmost value that is not repeated and put it into the new array
Function unique5 (array ){
Var r = [];
For (var I = 0, l = array. length; I <l; I ++ ){
For (var j = I + 1; j <l; j ++)
If (array [I] === array [j]) j = ++ I;
R. push (array [I]);
}
Return r;
}

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.