JS array deduplication

Source: Internet
Author: User

You have no time to test the following items, and you do not have time to implement them on your own. Record them first.

// First add the indexof and lastindexof methods to the array object prototype. (if not,) if (! Array. prototype. indexof) {array. prototype. indexof = function (element, index) {var length = This. length; If (Index = NULL) {Index = 0;} else {Index = + index | 0; If (index <0) index + = length; if (index <0) Index = 0;} For (VAR current; index <length; index ++) {current = This [Index]; If (current = element) return Index;} return-1 ;}} if (! Array. prototype. lastindexof) {array. prototype. lastindexof = function (element, index) {var length = This. length; If (Index = NULL) {Index = length-1;} else {Index = + index | 0; If (index <0) index + = length; if (index <0) Index =-1; else if (index> = length) Index = length-1;} For (VAR current; index> = 0; index --) {current = This [Index]; If (current = element) Return Index;} return-1 ;}// common implementation method var arrayunique1 = function (ARR) {for (VAR I = 0, Len = arr. length, result = [], item; I <Len; I ++) {item = arr [I]; If (result. indexof (item) <0) {result [result. length] = item ;}} return result ;}// use the lastindexof and splice methods to implement var arrayunique2 = function (ARR) {var length = arr. length; while (-- length) {// if it already exists, delete the element at this position if (ARR. lastindexof (ARR [length], length-1)>-1) {arr. splice (length, 1) ;}} return arr ;}

Test Results
Test data: var arr = [2,232,];
IE7 cycles 10,000 times:
Arrayunique1 is 460 ms, and arrayunique2 is 190 ms.
Ff3.5 cycles 100,000 times:
Arrayunique1 is 170 ms, and arrayunique2 is 63 MS.

Other implementation methods
In addition to the implementation method in 2 described above, there are actually other implementation methods. Jquery provides an implementation method. Let's take a look at the specific code:

unique: function( array ) {    var ret = [], done = {};    try {        for ( var i = 0, length = array.length; i < length; i++ ) {            var id = jQuery.data( array[ i ] );            if ( !done[ id ] ) {                done[ id ] = true;                ret.push( array[ i ] );            }        }    } catch( e ) {        ret = array;    }    return ret;}

From: http://www.welefen.com/javascript-array-unique.html

 

Array. prototype. delrepeat = function () {var newarray = new array (); var Len = This. length; For (VAR I = 0; I <Len; I ++) {for (var j = I + 1; j <Len; j ++) {If (this [I] === this [J]) {J = ++ I ;}} newarray. push (this [I]);} return newarray;} // but it is obvious that the for loop is embedded with another for loop. It must be time-consuming for a large amount of data! Inefficient! After searching and expert guidance, a new method is optimized: array. prototype. delrepeat = function () {var newarray = []; var provisionaltable ={}; for (VAR I = 0, item; (item = This [I])! = NULL; I ++) {If (! Provisionaltable [item]) {newarray. Push (item); provisionaltable [item] = true ;}} return newarray ;}

From: http://www.css88.com/archives/2429

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.