Deduplication is the removal of duplicate items from an array

1. Array traversal

Analysis of ideas:

Create a new [empty array], loop to get each item in the current array, and add to the empty array, check each time add, if there is an item in the new array do not add, do not add

    function unique(arr){
      1. Create an array
      let newArr = [];
      // 2. Iterate through the array
      for(let i = 0; i< arr.length; i++){// 3. Get the current item
        let item = arr[i];
        // 4. If the new array already has the current item, skip it
        // indexOf can also be used
        if(newArr.includes(item)){
          continue;
        }
        // 5. If the item is not included, push it to the new array
        newArr.push(item);
      }
      // 6. Return a new array
      return newArr;
    }

    let arr = [1.2.3.3.2.1.2.2.3];
    console.log(unique(arr));
Copy the code
function unique(arr) {
  var array = [];
  for (var i = 0; i < arr.length; i++) {
      // Use indexOf to determine whether the current item is included
    if (array.indexOf(arr[i]) === -1) {
      array.push(arr[i])
    }
  }
  return array;
}
Copy the code

2. Loop through the current array twice

Loop through each item in the array. Each item in the array is compared to the following item. If the value A is the same as A, splice will delete the item from the array

But there is the problem of array collapse

var arr = [1.2.3.1.2.1.2.3.2.1.2.3];
for(var i= 0; i<arr.length; i++){// 1. Get the current item
    var item = arr[i];
    // 2. Compare the current item with each subsequent item
    for(var j = i+1; j < arr.length; j++){// If the JTH term is identical to the current term
        if(arr[j] === item) {
            // Delete the current item from the array
            arr.splice(j,1);
            // The index after j is changed by one bit. The next index to compare is j
            // improve, j--j--; }}}Copy the code

3. Use objects

This method is similar to the first one, is the way to use objects, first create an empty object, and then go through the array, the value of each item of the array as an attribute, as well as the attribute value. If the same attribute is found in the object, the item is duplicated. Remove the item from the array. If no identical attribute is found, add it to the object.

var arr = [1.2.3.1.2.1.2.3.2.1.2.3];
// 1. Create an empty object
let obj = {}
// 2. Loop through each item in the array and store each item to the object
for(let i = 0; i< arr.length; i++){// Make a judgment before each storage: verify that obj has the current item
    let item = arr[i];
    if(obj[item] ! = =undefined) {Object already has the same value as this item, delete the item in array
        arr.splice(i,1);
        i--;
        continue;
    }
    // If not, place the attribute name and value in the object
    obj[item] = item;
}
Copy the code

Improvement: The performance of splice-based deletion is poor. After the current item is deleted, the index of each subsequent item must be one bit earlier than that of the previous one. If there are too many subsequent items, the performance will be affected.

var arr = [1.2.3.1.2.1.2.3.2.1.2.3];
// 1. Create an empty object
let obj = {}
// 2. Loop through each item in the array and store each item to the object
for(let i = 0; i< arr.length; i++){// Check before each storage: verify whether Obj exists
    let item = arr[i];
    if(obj[item] ! = =undefined) {// The improvement is to replace the current item with the last item, and then delete the last item so that you don't have to change many indexes
        arr[i] = arr[arr.length-1];
        arr.length--;
        i--;
        continue;
    }
    // If not, place the attribute name and value in the object
    obj[item] = item;

}
Copy the code

4. Sort first and then deduplicate

Sort the current array by sort.

function unique(arr) {
    / / 1. Sorting
      arr.sort((a,b) = > a-b)
    // 2. Create an array to store only the smallest number
      var arrry = [arr[0]].// 3. Iterate through the array at index 1, compare the current item with the previous one, and push the current item to the new array if different
      for (var i = 1; i < arr.length; i++) {
        if(arr[i] ! == arr[i -1]) { arrry.push(arr[i]); }}// 4. Return a new array
      return arrry;
    }
    let arr = [1.2.3.3.2.1.2.2.3];
    console.log(unique(arr));
Copy the code

5. ES6 set Deduplication

function unique(arr) {
  return Array.from(new Set(arr))
}
Copy the code