5 efficient algorithms for JavaScript to delete array repeating elements

Source: Internet
Author: User

1. Traversal Array method

The simplest way to do this is to create a new array, iterate through the incoming array, and add the value not to the new array; Note : The method of judging whether the value is in the array "IndexOf" is the ECMAScript5 method, IE8 below does not support, need to write some compatible low-version browser code, the source is as follows:

The simplest array de-reset function unique1 (array) {var n = [];//A new temporary array//iterates through the current array for (var i = 0; i < Array.Length; i++) {//if the current  The array I has been saved into a temporary array, then skip,//or push the current item to the temporary array inside if (N.indexof (array[i]) = =-1) n.push (Array[i]); } return n;}
//  Determines whether the browser supports indexof ,indexof  for EcmaScript5 new method  ie8 the following (including IE8,&NBSP;IE8 only supports partial ECMA5) does not support if   (! ARRAY.PROTOTYPE.INDEXOF) {  //  New 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;   }} 
2. Object Key-value pair method

The method executes faster than any other method, which is to occupy a larger amount of memory; implement the idea: 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, for example: a[1], a["1"]. To solve the above problem, you have to call "IndexOf".

  Fastest,  occupies the most space (space change 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);  &nbsP;      }    }    return r;} 
3.the judgment method of array subscript

Still have to call "IndexOf" performance is similar to Method 1, realize the idea : if the current array of item I in the current array the first occurrence of the position is not I, then the term I is repeated, ignored. Otherwise, the result array is deposited.

function Unique3 (array) {var n = [array[0]];//result array//start with the second entry for (var i = 1; i < Array.Length; i++) {//if the current array The first occurrence of the I item in the current array is not I,//Then it indicates that item I is duplicated and ignored.  Otherwise deposit the result array if (Array.indexof (array[i]) = = i) N.push (Array[i]); } return n;}
4.post-sorting adjacent removal method

Although the "sort" method of the native array is not very reliable, it has no effect on the shortcomings of the sequential de-emphasis. implementation ideas: sort incoming arrays, sort the same values next to each other, and then iterate through the array to add only values that do not duplicate the previous value.

The same value is adjacent and then traversed to remove the duplicate value 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 traversal Array method

Originating from a foreign blog, the implementation of this method is quite cool; realize the idea : Get the right-most value that is not duplicated into the new array. (The next round of judgment that terminates the current loop and enters the top loop when a duplicate value is detected)

Idea: Get the right-most value without duplicates 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;}

5 efficient algorithms for JavaScript to delete array repeating elements

Related Article

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.