Methods for removing duplicate values from arrays in JavaScript

Source: Internet
Author: User
Array deduplication is a common requirement. We will temporarily consider repeated arrays of the same type. It mainly aims to clarify ideas and considerations.

Array deduplication is a common requirement. We will temporarily consider repeated arrays of the same type. It mainly aims to clarify the ideas and consider the performance. The following methods are available on the Internet. Here is a brief summary.

Ideas:

  1. Traverse the array, compare them one by one, and delete
  2. Traverse the array, compare them one by one, compare them to the same one, skip the previous duplicate, and put the different into the new array
  3. Take an array element and add it to the new array. traverse the remaining array elements and compare them with the elements of the new array. If there are different elements, add them to the new array.
  4. Traverse the array and take an element as the object attribute to determine whether the attribute exists.

1. Delete the following duplicate:

Function ov1 (arr) {// var a1 = (new Date). getTime () for (var I = 0; I

2. This is a conventional method and is easy to understand. If the same method is used, the loop jumps out.

function ov2(a) {    //var a1=((new Date).getTime())    var b = [], n = a.length, i, j;    for (i = 0; i < n; i++) {        for (j = i + 1; j < n; j++)            if (a[i] === a[j]){j=false;break;}        if(j)b.push(a[i]);        }    //console.info((new Date).getTime()-a1)        return b.sort(function(a,b){return a-b});} 

3. It took me a long time to understand this. Although the j loop continues here, the I value has changed. It is like a new I loop:

function ov3(a) {    //var a1=((new Date).getTime())    var b = [], n = a.length, i, j;    for (i = 0; i < n; i++) {        for (j = i + 1; j < n; j++)        if (a[i] === a[j])j=++i    b.push(a[i]);}    //console.info((new Date).getTime()-a1)        return b.sort(function(a,b){return a-b});}     

4. Ensure that the new array is unique.

Function ov4 (ar) {// var a1 = (new Date). getTime () var m = [], f; for (var I = 0; I

5. Use object attributes

function ov5(ar){//    var a1=(new Date).getTime()var m,n=[],o= {};for (var i=0;(m= ar[i])!==undefined;i++)if (!o[m]){n.push(m);o[m]=true;}//    console.info((new Date).getTime()-a1)    return n.sort(function(a,b){return a-b});;}

Address of this article: http://www.nowamagic.net/librarys/veda/detail/1487,welcome.

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.