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

JavaScript array deduplication algorithm example

一个新手
Release: 2018-05-23 14:56:38
Original
2446 people have browsed it

This article mainly introduces the JavaScript array deduplication algorithm. It summarizes and analyzes the reading, writing, traversing, comparison, sorting and other operations related to JavaScript array deduplication as well as implementation techniques related to algorithm improvement in the form of examples. Friends in need can refer to the following

The examples in this article summarize the JavaScript array deduplication algorithm. Share it with everyone for your reference, the details are as follows:

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 Differentiate objects

Method 2: Use the Object object in JavaScript as a hash 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"

Modify

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

Above I compiled it for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Ajax transfers JSON example code

ajax uses json to realize data transmission

Ajax check whether duplicate implementation code

The above is the detailed content of JavaScript array deduplication algorithm example. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!