Js array de-duplication 5 algorithm implementations _ javascript skills

Source: Internet
Author: User
This article mainly introduces the implementation of five js array deduplication algorithms, explains the Implementation ideas in detail, and the advantages and disadvantages of each algorithm. If you are interested, you can refer 1. traverse the Array Method
The simplest deduplication method, Implementation: 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, IE8 is not supported. You need to write more code compatible with earlier versions of browsers. The source code is as follows:

// Function unique1 (array) {var n = []; // a new temporary array // traverses the current array for (var I = 0; I <array. length; I ++) {// if the I of the current array has been saved in 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 ;}

2. Object key-Value Pair Method
The execution speed of this method is faster than that of any other method, that is, the memory occupied is larger,Implementation: 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, 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 ;}

3. array subscript Limit Method
You still have to call "indexOf" to achieve performance similar to method 1,Implementation: 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 // traverses 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, // It indicates that the I entry is repeated, ignore. Otherwise, the result array if (array. indexOf (array [I]) = I) n. push (array [I]) ;}return n ;}

4. Adjacent removal method after sorting
Although the sorting result of the native array's "sort" method is not very reliable, this disadvantage has no effect in removing duplicates without paying attention to order.Implementation: Sorts the input array, and the same values are adjacent to each other after sorting. When traversing, the new array only adds values that are not repeated with the previous values.

// Adjacent the same value, and traverse the 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 ;}

5. optimized the traversal array method.
The implementation code of this method is cool,Implementation: 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) recommended

// 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 ;}

Determine whether the browser supports indexOf. If indexOf is ecmaScript5, the new method IE8 or lower (IE8 and IE8 only support part of ecma5) is not supported.

If (! Array. prototype. indexOf) {// added the indexOf method Array. prototype. indexOf = function (item) {var result =-1, a_item = null; if (this. length = 0) {return result;} for (var I = 0, len = this. length; I <len; I ++) {a_item = this [I]; if (a_item = item) {result = I; break ;}} return result ;}}

The above is the implementation of the five JS array de-duplication algorithms provided for everyone, and I hope to help everyone learn.

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.