JS Array de-weight

Source: Internet
Author: User

The first is a more conventional approach.

Ideas:

1. Build a new array to store the results

Each time an element is removed from the original array in a 2.for loop, the loop is compared with the result array.

3. If the element is not in the result array, it is stored in the result array

1Array.prototype.unique1 =function(){2  varres = [ this[0]];3   for(vari = 1; I < this. length; i++){4   varrepeat =false;5    for(varj = 0; J < res.length; J + +){6    if( this[i] = =Res[j]) {7repeat =true;8      break;9    }Ten   } one   if(!Repeat) { aRes.push ( this[i]); -   } -  } the  returnres; - } - vararr = [1, ' A ', ' a ', ' b ', ' d ', ' e ', ' e ', 1, 0] -Alert (arr.unique1 ());

The second method is more efficient than the above method

Ideas:

1. Sort the original array first

2. Check that the first element in the original array is the same as the last element in the result array, because it is already sorted, so the repeating element is in the adjacent position

3. If it is not the same, the element is stored in the result array

1Array.prototype.unique2 =function(){2   this. Sort ();//Sort First3  varres = [ this[0]];4   for(vari = 1; I < this. length; i++){5   if( this[i]!== res[res.length-1]){6Res.push ( this[i]);7   }8  }9  returnres;Ten } one vararr = [1, ' A ', ' a ', ' b ', ' d ', ' e ', ' e ', 1, 0] aAlert (arr.unique2 ());

The second method also has some limitations because it is sorted before going to heavy, so the final return of the de-weight result is also Sorted. This method is not available if the order of the array is not changed to be heavy.

Third Method (recommended)

Ideas:

1. Create a new array to hold the result

2. Create an empty object

3.for loop, Each time an element is taken out and compared to the object, if the element is not duplicated, it is stored in the result array, and the content of the element as an object property, and assigned a value of 1, deposited in the 2nd step of the object Created.

Note: as for how to compare, it is to remove an element from the original array, and then go to the object to access the property, if you can access the value, then the description is Repeated.

1Array.prototype.unique3 =function(){2  varres = [];3  varJSON = {};4   for(vari = 0; I < this. length; i++){5   if(!json[ this[i]]) {6Res.push ( this[i]);7json[ this[i]] = 1;8   }9  }Ten  returnres; one } a vararr = [112,112, 34, ' Hello ', 112,112, 34, ' Hello ', ' str ', ' str1 ']; -Alert (arr.unique3 ());

Fourth method

1 vararr = [1, 2, 3, 3, 4, 4, 5, 5, 6, 1, 9, 3, 25, 4];2 functionderepeat () {3varNEWARR = [];4varobj = {};5 varindex = 0;6varL =arr.length;7 for(vari = 0; I < l; i++) {8if(obj[arr[i]] = =Undefined) {9obj[arr[i]] = 1;Tennewarr[index++] =arr[i]; one}Elseif(obj[arr[i]] = = 1) aContinue; -     } -returnnewArr; the } - varNEWARR2 =derepeat (arr); -Alert (newArr2);//Output 1,2,3,4,5,6,9,25

JS Array de-weight

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.