Home > Web Front-end > JS Tutorial > Various methods to achieve array deduplication in javascript_javascript skills

Various methods to achieve array deduplication in javascript_javascript skills

WBOY
Release: 2016-05-16 15:10:54
Original
1429 people have browsed it

No further nonsense, let’s get straight to the practical stuff!

Let’s first talk about the requirements of this example: write a method to achieve deduplication of the array. (Requirements: Execute method, pass an array, return the new array after deduplication, the original array remains unchanged, only one layer of loops can be used during the implementation process, double-layer nested loops can also be written, for reference only);

First, let me explain to beginners what array deduplication is (skip for veterans): it means to remove duplicate elements in the array, for example, var arr = [3,2,4,2,1,2]; The new array obtained by deduplicating the array is [3,2,4,1], which is such a function.

The implementation method is relatively simple, and there are many ways to implement it. Many experts have also written related articles. The purpose of writing this blog is firstly to serve as a reminder, and secondly to give beginners a better understanding of it. The principle of implementation, okay, let’s look at the first implementation method:

The first method is to remove duplicates by traversing the new array

var arr = [1,'b','b',4,3,3,4,5,1];
     
    //第一种
    Array.prototype.unique1 = function(){
      var arr1 = []; //定义一个新数组
      for(var i=0;i<this.length;i++){
        if(arr1.indexOf(this[i]) == -1){//判断目标数组中在原数组里是否存在
          arr1.push(this[i]);
        } 
      } 
      return arr1;
    }
    console.log(arr); //[1,'b','b',4,3,3,4,5,1]
    console.log(arr.unique1()); //[1, "b", 4, 3, 5]
    //这种方法的主要思路就是,新建一个数组,然后在原数组中,从第一个开始,看看新数组里面有没有这个元素,如果有,就忽略,然后进行下一个,如果没有,则把这个元素存到新数组里面,
    //也就是说,每一次比较,都会遍历新数组,直到找到相同元素为止,比较耗性能
Copy after login

If you are not used to this writing method, you can change it to the following writing method, the effect is the same:

var arr = [1,'b','b',4,3,3,4,5,1];

function unique1(arr){
      var arr1 = [];
      for(var i=0;i<arr.length;i++){
        if(arr1.indexOf(arr[i]) == -1){//判断目标数组中在原数组里是否存在
          arr1.push(arr[i]); 
        } 
      } 
      return arr1;
    }
    console.log(arr); //[1,'b','b',4,3,3,4,5,1]
    console.log(unique1(arr)); //[1, "b", 4, 3, 5]

Copy after login

I will not rewrite the following method. You can rewrite it according to the above format. I will not output the result because the result is the same. The comments are written in the code. Take your time to experience it

The second type is achieved through hash tables (this concept is a bit big, the specific principles will not be elaborated here. I will write it separately when I have time. This is a good thing)

var arr = [1,'b','b',4,3,3,4,5,1];

Array.prototype.unique2 = function(){
      var hash = {}; //定义一个hash表
      var arr1 = []; //定义一个新数组
      for(var i=0;i<this.length;i++){
        /*
          这里比较难理解,我们一步一步来看:
          hash是一个对象,则存在键值对(key:value),只不过现在是为空的,所以hash[key] = value;
          第一步:i=0;this[i]=this[0]=1; hash[this[0]] = hash[1] , 因为hash初始为空,没有找到key=1的值,所以然后undefined,
          执行下一步:hash[1] = true(此时hash对象就有了第一组键值对),将原数组的第一个数添加到新数组中,重复第一步
          因为不重复的判断hash的值都是undefined,而重复的都为true了,所以不重复都被添加到新数组中
          因为hash表存的值是存的地址,放在堆内存中,所以有多少个不重复的元素,就要分多少个内存来存放,所以这种方法比较占内存,但是相比之下,这种的运算运动是最快的,
          这也就是用空间来换取时间了,数据量比较小,推荐用此方法
        */
        if(! hash[this[i]]){
          hash[this[i]] = true;
          arr1.push(this[i]);
        }
      }
      return arr1;  
    }
    console.log(arr);
    console.log(arr.unique2());
Copy after login


The third method is achieved by traversing whether its own position is consistent

var arr = [1,'b','b',4,3,3,4,5,1];

Array.prototype.unique3 = function(){
      var arr1 = []; //定义一个新数组
      for(var i=0;i<this.length;i++){
        if(this.indexOf(this[i])==i){
        //这里也是indexOf遍历,看从第一个元素在原数组中的位置,如果第一次出现的位置和下标相等,说明当前元素的不重复的,如果不等,说明该元素前面已经出现过
          arr1.push(this[i]);
        }
      }
      return arr1;  
    }
    console.log(arr);
    console.log(arr.unique3());
Copy after login


The fourth method, which is a bit interesting, can only be used in special occasions. It is to sort the array first, then compare 22, and output a new sorted array

Array.prototype.unique4 = function(){
      /*
        这里是思路是,先排序(默认从小到大),然后将原数组的第一个给新数组,
        因为是经过排序的,所以重复的只会存在在相邻位置
        这里就相当于是做22比较,如果相等,则进行下一组,如果不相等,则把这个数存到新数组中,用这个数再进行比较
      */
      this.sort();
      var arr1 = [this[0]];
      for(var i=1;i<this.length;i++){
        if(this[i] !== arr1[arr1.length-1]){
          arr1.push(this[i]);
        } 
      }
      return arr1;  
    }
    console.log(arr);
    console.log(arr.unique4());
Copy after login

Wow, let’s call it a day!

The requirements also say that it can be implemented using a double-layer nested loop. The other way around is to use a 2-layer for loop and compare each one with the original array

Array.prototype.unique5 = function(){
      //双层循环,一一比较
      for(var i=0;i<this.length;i++){ //从0开始
        for(j= i+1;j<this.length;j++){ //从1开始,逐个比较
          if(this[i] === this[j]){ //如果恒定
            this.splice(j,1);  //就将这个元素删掉
          } 
        } 
      }
      return this;  
    }
    console.log(arr);
    console.log(arr.unique5());
Copy after login

This writing method requires too many loops and is not recommended. Some people will say, don’t the first and third methods also need to be traversed every time? Is it similar to the fifth method? Yes, you can understand it this way, which means you understand it, but it is not a special understanding. If we say it is almost the same, it is too different. indexOf() means that when the first matching element is found, it will be

Stop traversing, and Type 5 will traverse the entire array regardless of whether it can be found. If the amount of data is large, which one do you think has better performance?

A special note: When comparing two values ​​that are congruent or unequal, be sure to use constant (===) and non-constant (!==), because this will involve elements In terms of type, for example, 1 and '1' are not identical!

The above is really dry information, there is no moisture at all, you can only rely on everyone to understand it!

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