Implementation of JS array de-weight algorithm

Source: Internet
Author: User

reprint: https://github.com/wteam-xq/testDemo/blob/master/array.html 1. Traversal Array Method

The simplest method of de-weight, realizes the idea: Create a new array, iterate through the array, the value is not added to the new array in the new array; Note: The method of judging whether the value is in the array "IndexOf" is the ECMAScript5 method, IE8 the following is not supported, you need to write some compatible low-version browser code, The source code is as follows:

 //  simplest array de-weight    Unique1 (array) { var  n = [ ]; //  a new temporary array  //  traverse the current array  for  (var  i = 0; i < array.length; I++) { //  //   if  (n.indexof (Array[i]) = = -1 return   N;}  
//determine if the browser supports INDEXOF, IndexOf is the EcmaScript5 new method IE8 the following (including IE8, IE8 only support partial ECMA5) does not supportif(!Array.prototype.indexOf) {//New IndexOf MethodArray.prototype.indexOf =function(item) {varresult =-1, A_item =NULL;if( This. length = = 0){returnresult;} for(vari = 0, Len = This. length; i < Len; i++) {A_item= This[i];if(A_item = = =Item) {Result=i; Break;}}returnresult;}}

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 , occupying the most space (space change time)functionunique2 (array) {varn = {}, R = [], Len =Array.Length, Val, type; for(vari = 0; i < Array.Length; i++) {Val=Array[i];type=typeofVal;if(!N[val]) {N[val]=[Type];r.push (val);} Else if(N[val].indexof (type) < 0{N[val].push (type); R.push (val);}}returnR;}

3. Array subscript Judgment method

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// result array // start with the second entry for the for (var i = 1; i < Array.Length; i++) {// If the item I of the current array first appears in the current array is not I,// Then the term 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.

// next to the same value, and then 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;}

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

Implementation of JS array de-weight algorithm

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.