Algorithm of removing duplicate data in Js array

In JS, we often meet the need of removing duplicate data from arrays. In this paper, we introduce four algorithms to realize the function of JS array de duplication

1. Fastest algorithm: object key value pair method

Implementation ideas:
Create a js object and a new array. When traversing the incoming array, judge whether the value is the key of the js object. If not, add the key to the object and put it into the new array.

/*
Note: when judging whether it is a js object key, it will automatically execute "toString()" on the incoming key, and different keys may be mistaken for the same;
For example: a[1], a["1"]. To solve the above problem, you need to call "indexOf".
Fastest, most space (space for time)
*/
function unique(array){
      var n = {}, r = [], len = array.length, val, type;
      for (var i = 0; i < array.length; i++) {
          val = array[i];
          type = typeof val;
          if (!n[val]) {
              n[val] = [type];
              r.push(val);
          } else if (n[val].indexOf(type) < 0) {
              n[val].push(type);
              r.push(val);
          }
      }
      return r;
  }

2. The most ingenious algorithm: optimized traversal array method

Implementation idea: get the right most value without repetition and put it into the new array. (terminate the current cycle and enter the next round of the top cycle when duplicate values are detected)

function unique1(array){
    var r = [];
    for(var i = 0, l = array.length; i<l; i++){
        for(var j = i + 1; j < l; j++)
            if(array[i] == array[j]) j == ++i;
        r.push(array[i]);
    }
    return r;
}

3. Adjacent division after sorting

Implementation idea: sort the incoming array. After sorting, the same value is adjacent to each other. Then, when traversing, the new array only adds values that do not repeat the previous value.

//Adjacent the same value, and then traverse to remove duplicate values
function unique2(array){
     array.sort();
     var re=[array[0]];
     for(var i = 1; i < array.length; i++){
         if( array[i] !== re[re.length-1])
         {
             re.push(array[i]);
         }
     }
     return re;
 }

4. Array subscript judgment

Implementation idea: if the position of item i of the current array is not i for the first time in the current array, then it means that item i is repeated and ignored. Otherwise, it will be stored in the result array

function unique3(array){
    var n = [array[0]]; //Result array
    //Traverse from the second term
    for(var i = 1; i < array.length; i++) {
        //If item i of the current array first appears in the current array at a position other than i,
        //Then it means that item i is repeated and ignored. Otherwise, it will be stored in the result array
        if (array.indexOf(array[i]) == i) n.push(array[i]);
    }
    return n;
}

Reprint blog: https://blog.csdn.net/qiuxuewei2012/article/details/52837083

Posted by kkeim on Thu, 26 Dec 2019 12:17:36 -0800