JS through the array, the array to achieve a weight (duplicate elements but one)

 

1. iterate method

It is the most simple array deduplication method (indexOf method)

Realization of ideas: a new array to traverse the array to be heavy, when a new value is not in the array (the indexOf -1) was added to the new array;

Copy the code
var arr=[2,8,5,0,5,2,6,7,2];
function unique1(arr){
  var hash=[];
  for (var i = 0; i < arr.length; i++) {
     if(hash.indexOf(arr[i])==-1){
      hash.push(arr[i]);
     }
  }
  return hash;
}
Copy the code

2. Analyzing Method array subscript

Call indexOf method, performance, and almost 1

Realization of ideas: if the current array of item i for the first time in the current array position i is not, then i represents the item is repeated, ignored. Otherwise, the result is stored in an array.

Copy the code
function unique2(arr){
  var hash=[];
  for (var i = 0; i < arr.length; i++) {
     if(arr.indexOf(arr[i])==i){
      hash.push(arr[i]);
     }
  }
  return hash;
}
Copy the code

3. Sort the adjacent removal method 

Realization of ideas: to sort the array passed, after ordering the same value will be adjacent, then traverse an array after sorting, adding only new array does not overlap with the previous value.

Copy the code
function unique3(arr){
  arr.sort();
  var hash=[arr[0]];
  for (var i = 1; i < arr.length; i++) {
     if(arr[i]!=hash[hash.length-1]){
      hash.push(arr[i]);
     }
  }
  return hash;
}
Copy the code

4. iterate the optimization method (Reference)

Realization of ideas: double loop, outer loop represents from 0 to arr.length, the cycle represents the i + 1 to arr.length

Will not duplicate the right values ​​into a new array. (Detected at a current cycle terminates when the outer loop repeats the same time into the value of the judgment)

Copy the code
function unique4(arr){
  var hash=[];
  for (var i = 0; i < arr.length; i++) {
    for (var j = i+1; j < arr.length; j++) {
      if(arr[i]===arr[j]){
        ++i;
      }
    }
      hash.push(arr[i]);
  }
  return hash;
}
Copy the code

5.ES6 achieve

The basic idea: ES6 provides a new data structure Set. It is similar to an array, but the value of the member is unique, no duplicate values.

Set function can accept an array (or array-like objects) as parameters, it is used to initialize.

function unique5(arr){
  var x = new Set(arr);
 return [...x];
}

 Extended: If repeated, this element is removed

Array subscript deduplication

Copy the code
function unique22(arr){
  var hash=[];
  for (var i = 0; i < arr.length; i++) {
     if(arr.indexOf(arr[i])==arr.lastIndexOf(arr[i])){
      hash.push(arr[i]);
     }
  }
  return hash;
}
Copy the code

1. iterate method

It is the most simple array deduplication method (indexOf method)

Realization of ideas: a new array to traverse the array to be heavy, when a new value is not in the array (the indexOf -1) was added to the new array;

Copy the code
var arr=[2,8,5,0,5,2,6,7,2];
function unique1(arr){
  var hash=[];
  for (var i = 0; i < arr.length; i++) {
     if(hash.indexOf(arr[i])==-1){
      hash.push(arr[i]);
     }
  }
  return hash;
}
Copy the code

2. Analyzing Method array subscript

Call indexOf method, performance, and almost 1

Realization of ideas: if the current array of item i for the first time in the current array position i is not, then i represents the item is repeated, ignored. Otherwise, the result is stored in an array.

Copy the code
function unique2(arr){
  var hash=[];
  for (var i = 0; i < arr.length; i++) {
     if(arr.indexOf(arr[i])==i){
      hash.push(arr[i]);
     }
  }
  return hash;
}
Copy the code

3. Sort the adjacent removal method 

Realization of ideas: to sort the array passed, after ordering the same value will be adjacent, then traverse an array after sorting, adding only new array does not overlap with the previous value.

Copy the code
function unique3(arr){
  arr.sort();
  var hash=[arr[0]];
  for (var i = 1; i < arr.length; i++) {
     if(arr[i]!=hash[hash.length-1]){
      hash.push(arr[i]);
     }
  }
  return hash;
}
Copy the code

4. iterate the optimization method (Reference)

Realization of ideas: double loop, outer loop represents from 0 to arr.length, the cycle represents the i + 1 to arr.length

Will not duplicate the right values ​​into a new array. (Detected at a current cycle terminates when the outer loop repeats the same time into the value of the judgment)

Copy the code
function unique4(arr){
  var hash=[];
  for (var i = 0; i < arr.length; i++) {
    for (var j = i+1; j < arr.length; j++) {
      if(arr[i]===arr[j]){
        ++i;
      }
    }
      hash.push(arr[i]);
  }
  return hash;
}
Copy the code

5.ES6 achieve

The basic idea: ES6 provides a new data structure Set. It is similar to an array, but the value of the member is unique, no duplicate values.

Set function can accept an array (or array-like objects) as parameters, it is used to initialize.

function unique5(arr){
  var x = new Set(arr);
 return [...x];
}

 Extended: If repeated, this element is removed

Array subscript deduplication

Copy the code
function unique22(arr){
  var hash=[];
  for (var i = 0; i < arr.length; i++) {
     if(arr.indexOf(arr[i])==arr.lastIndexOf(arr[i])){
      hash.push(arr[i]);
     }
  }
  return hash;
}
Copy the code

Guess you like

Origin www.cnblogs.com/lmyupupblogs/p/11085351.html