A method of removing duplicate values from several arrays of JavaScript

Source: Internet
Author: User

Array de-duplication is a common requirement, we temporarily consider the same type of array to repeat. It is mainly to clarify the thinking and consider the performance. The following methods, basic online, here is simply a summary.

Ideas:

    1. Iterate over the array, compare each, compare to the same to delete the later Tuanfeng County University
    2. Iterate over the array, compare each, compare to the same, skip the previous duplicate, and put the new array in a different way
    3. Either takes an array element into the new array, iterates over the remaining array elements, and compares them with the elements of the new array, and if there is a different one, put the new array.
    4. Iterate through an array, take an element, act as an object's property, and determine whether a property exists

1. Delete the following duplicates:

View Source print?
1 functionov1(arr){
2     //var a1=((new Date).getTime())
3     for(vari=0;i<arr.length;i++)
4         for(varj=i+1;j<arr.length;j++)
5             if(arr[i]===arr[j]){arr.splice(j,1);j--;}           
6     //console.info((new Date).getTime()-a1)               
7     returnarr.sort(function(a,b){returna-b});
8 }

2. This is a conventional method, better understanding, if the same jump out of the loop

View Source print?
01 functionov2(a) {
02     //var a1=((new Date).getTime())
03     varb = [], n = a.length, i, j;
04     for(i = 0; i < n; i++) {
05         for(j = i + 1; j < n; j++)
06             if(a[i] === a[j]){j=false;break;}
07         if(j)b.push(a[i]);
08         }
09     //console.info((new Date).getTime()-a1)   
10     returnb.sort(function(a,b){returna-b});
11 }

3. It took me a long time to understand that the J Loop continues, but the I value has changed. is equal to a new I loop:

View Source print?
01 functionov3(a) {
02     //var a1=((new Date).getTime())
03     varb = [], n = a.length, i, j;
04     for(i = 0; i < n; i++) {
05         for(j = i + 1; j < n; j++)
06         if(a[i] === a[j])j=++i
07     b.push(a[i]);}
08     //console.info((new Date).getTime()-a1)   
09     returnb.sort(function(a,b){returna-b});
10 }    

4. Ensure that the new array is unique

View Source print?
01 functionov4(ar){
02 //var a1=((new Date).getTime())
03     varm=[],f;
04     for(vari=0;i<ar.length;i++){
05     f=true;
06     for(varj=0;j<m.length;j++)
07     if(ar[i]===m[j]){f=false;break;};
08     if(f)m.push(ar[i])}
09 //console.info((new Date).getTime()-a1)   
10     returnm.sort(function(a,b){returna-b});
11 }

5. Using Object properties

View Source print?
1 functionov5(ar){
2 //    var a1=(new Date).getTime()
3         varm,n=[],o= {};
4         for(vari=0;(m= ar[i])!==undefined;i++)
5         if(!o[m]){n.push(m);o[m]=true;}
6 //    console.info((new Date).getTime()-a1)   
7     returnn.sort(function(a,b){returna-b});;
8     }

A method of removing duplicate values from several arrays of JavaScript

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.