Home > Web Front-end > JS Tutorial > body text

How to implement array deduplication algorithm in JS

php中世界最好的语言
Release: 2018-05-23 11:46:01
Original
1578 people have browsed it

This time I will show you how to implement the array deduplication algorithm in JS. What are the precautions for implementing the array deduplication algorithm in JS? The following is a practical case, let's take a look.

Test case:

arr = ["1",3,"1",1,4,5,1,"2",5,1,{"name ":"li","age":20},2,4,3,{"name":"li","age":20},""];

Method 1: With the help of temporary array and indexOf, The algorithm complexity is: O(n^2)

function unique1(arr){
  var temp = [];
  for(var i=0; i<arr.length; i++){
    if(temp.indexOf(arr[i]) == -1){
      temp.push(arr[i]);
    }
  }
  return temp;
}
Copy after login
Test result:

unique1(arr): ["1", 3, 1, 4, 5, "2", Object { name="li", age=20}, 2, Object { name="li", age=20}, ""]

bug Unable to distinguish objects

Method 2: Use Object object in JavaScript as Ha Greek table

function unique2(arr){
  var temp=[];
  var hash={};
  for(var i=0; i<arr.length;i++){
    if(!hash[arr[i]]){
      hash[arr[i]]=true;
      temp.push(arr[i]);
    }
  }
  return temp;
}
Copy after login
Test result:

unique2(arr): ["1", 3, 4, 5, "2", Object { name="li ", age=20}, ""]

bug: Unable to distinguish: 1 and "1"

Modification

function unique2(arr){
  var temp=[];
  var hash={};
  for(var i=0; i<arr.length;i++){
      var item = arr[i];
    var key = typeof(item)+item;
    if(!hash[key]){
      hash[key]=true;
      temp.push(arr[i]);
    }
  }
  return temp;
}
Copy after login
Test result:

unique2(arr): ["1", 3, 1, 4, 5, "2", Object { name="li", age=20}, 2, ""]

Method 3: First use sort to sort the array, and then use a temporary array to store the last one of the same element. This method can only be used for pure Number type arrays

function unique3(arr){
  arr.sort(function(a,b){
    return a-b;
  });
  var temp = [];
  for(var i=0;i<arr.length;i++){
    if(arr[i] !== arr[i+1]){
      temp.push(arr[i]);
    }
  }
  return temp;
}
Copy after login
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use Vue better-scroll to implement alphabetical index navigation

How to implement Vue WeChat project on demand Authorized login

The above is the detailed content of How to implement array deduplication algorithm in JS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template