Array deduplication method

Sometimes there are duplicate elements in an array, and we don't need to deduplicate the array because of this.

There are many ways to deduplicate, here are a few methods that are easy to understand.

1. The easiest way first

Take advantage of the new data structure set provided by ES6. It is similar to an array, but its element values ​​are unique and not repeated.

1 let arr = [1,1,1,1,1,1,2,3,4,1,2,3,4]
2 arr = Array.from(new Set(arr))
3 console.log(arr)
4 // [ 1, 2, 3, 4 ]

2. Use the indexOf() method of the string. This method searches a string for a given substring, then returns the position of the substring (or -1 if not found).

Arrays can also be used.

let arr = [1,1,1,1,1,1,2,3,4]

function distinct(arr){
  let result = [],len=arr.length;
  arr.forEach((v, i, arr) => {
    var bool = arr.indexOf(v, i+1);
    if(bool === -1){
      result.push(v);
    }
  });
  return result;
}
console.log(distinct(arr))
// [ 1, 2, 3, 4 ]

3. Use the splice() method of the array. The double loop judges whether it is equal, and if it is equal, the latter one is deleted. But it will change the original array.

 1 let arr = [1,1,1,1,1,1,2,3,4]
 2 
 3 function distinct(arr) {
 4   let i, j, len=arr.length;
 5   for( i=0;i<len;i++){
 6     for(j=i+1;j<len;j++){
 7       if(arr[i] == arr[j]){
 8         arr.splice(j,1);
 9         len--;
10         j--;
11       }
12     }
13   }
14   return arr;
15 }
16 console.log(distinct(arr))
17 // [ 1, 2, 3, 4 ]

4. Use the filter method and the existence of object properties to remove duplicates.

let arr = [1,1,1,1,'1',1,2,3,4,'4']

function unique(arr){
  let obj = {};
  return arr.filter((item) => {
    return obj.hasOwnProperty(item)?false:(obj[item]=true);
  })
}
console.log(unique(arr))
// [ 1, 2, 3, 4 ]

As can be seen from the above, 1 and '1' cannot be distinguished, so this method is only suitable for arrays of pure numbers.

5. Whether the attribute value of the use object exists

let arr = [1,1,1,1,1,1,2,3,4,'4','4','4']

function unique(arr){
  let obj={},
  item,key,
  res = [];
  for(let i=0,len=arr.length;i<len;i++){
    item = arr[i];
    key = typeof (item) + item;
    if(!obj[key]){
      res.push(item);
      obj[key] = item;
    }
  }
  return res;
}
console.log(unique(arr))
// [ 1, 2, 3, 4, '4' ]

This method can distinguish between numbers and characters and is better than the previous method.

6. Conventional method. The double loop checks whether the elements are equal.

let arr = [1,1,1,1,1,1,2,3,4,'4','4','4']

function distinct(arr) {
  var result  = [];
  for(let i=0,len=arr.length;i<len;i++){
    for(let j=i+1;j<len;j++){
      if(arr[i] === arr[j]) {
        j = ++i
        // console.log(j)
      }
    }
    result.push(arr[i])
  }
  return result;
}
console.log(distinct(arr))
// [ 1, 2, 3, 4, '4' ]

The above are a few easy-to-understand common array deduplication methods.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325028195&siteId=291194637