Because JS to redo can use a variety of methods to achieve, so in the interview is often used to inspect, you grasp the comprehensive degree of knowledge. Most of the time, I think of one or two methods at the beginning, and then I recall the problem carefully and feel that there are more than one or two ways to achieve the problem. Take a look. You can think of a few.

1. Use the Set method in ES6 to remove weights

Note: Set is a new object in ES6 that allows you to store unique values of any type (raw or reference)

1 / / method
let arr = [1.0.0.2.4.9.8.3.1];

function unique(arr) {
    return Array.from(new Set(arr))
}
console.log(unique(arr));   / /,0,2,4,9,8,3 [1]
Copy the code
2 / / method
let arr = [1.0.0.2.4.9.8.3.1];

console.log(... newSet(arr));/ /,0,2,4,9,8,3 [1]
Copy the code

2. Use the Map method in ES6 to remove weights

/* Create an empty Map data structure that iterates through the array to be de-duplicated, storing each element of the array as a key in the Map. Since the Map does not have the same key value, the final result is the result after the deduplication. * /
let arr = [1.0.8.3, -9.1.0, -9.7]
function unique(arr) {
 let map = new Map(a);console.log(map)
 //let arr1 = new Array(); // The array is used to return the result
 let arr1 = []
     for (let i = 0, len = arr.length; i < len; i++) {
         if (map.has(arr[i])) {      // Check whether the key exists
             map.set(arr[i], true);
         }
         else {
             map.set(arr[i], false); arr1.push(arr[i]); }}return arr1;
 }
console.log(unique(arr)); // 1, 0, 8, 3, -9, 7
Copy the code

3, use the array filter method to remove the weight

Array. filter(function(currentValue,index,arr), thisValue)

CurrentValue: value of the current element (mandatory), index: index of the current element (optional), ARr: array object to which the current element belongs (optional), thisValue: Object used for this callback, passed to the function, as the value of “this”, undefined by default (optional)

var arr = [1.2.8.9.5.8.4.0.4];
 / * simulation: Original array: [1,2,8,9,5,8,4,0,4] index: 0,1,2,3,4,5,6,7,8 pseudo-new array: [1,2,8,9,5,8,4,0,4] 0,1,2,3,4,2,6,7,6 return index values before and after the same elements: new array: * /,2,8,9,5,4,0 [1]
function unique( arr ){
     // If the index of the current element of the new array == the first index of that element in the original array, the current element is returned
     return arr.filter(function(item,index){
         return arr.indexOf(item,0) === index;
     });
 }
 console.log(unique(arr));    // 1, 2, 8, 9, 5, 4, 0
Copy the code

Use hasOwnProperty

Use hasOwnProperty to determine whether an object property exists

function unique(arr) {
    let obj = {};
    return arr.filter(function(item, index, arr){
        return obj.hasOwnProperty(typeof item + item) ? false : (obj[typeof item + item] = true)})}let arr = [1.0.0.2.4.4.8.3.1];
console.log(unique(arr));   / /,0,2,4,8,3 [1]
Copy the code

5, use the array indexOf method to remove the weight

Array.indexof (item,statt) returns the position of a specified element in the array, or -1 if not

var arr =[1, -5, -4.0, -4.7.7.3];
function unique(arr){
   var arr1 = [];       // Create an array to store the values in the arR
   for(var i=0,len=arr.length; i<len; i++){if(arr1.indexOf(arr[i]) === -1){ arr1.push(arr[i]); }}return arr1;
}
console.log(unique(arr));    // 1, -5, -4, 0, 7, 3
Copy the code

6, Use the array sort method to remove weight (adjacent element comparison method)

Note: The array.sort(function) argument must be a function, optional, ascending by default

var arr =  [5.7.1.8.1.8.3.4.9.7];
function unique( arr ){
    arr = arr.sort();
    console.log(arr);

    var arr1 = [arr[0]].for(var i=1,len=arr.length; i<len; i++){if(arr[i] ! == arr[i-1]){ arr1.push(arr[i]); }}return arr1;
}
console.log(unique(arr))l;   // 1, 1, 3, 4, 5, 7, 7, 8, 8, 9
Copy the code

7, use the array includes to delete

Note: Arr.includes (specifies elements (mandatory), specifies index values (optional, default is 0)), returns true if there is any value, false if there is none

function unique( arr ){
  var arr1 = [];
  for(var i=0,len=arr.length; i<len; i++){if( !arr1.includes( arr[i] ) ){      // Retrieve arR1 to see if it contains arR valuesarr1.push(arr[i]); }}return arr1;
}

let arr = [1.0.0.2.4.4.8.3.1];
console.log(unique(arr));   / /,0,2,4,8,3 [1]
Copy the code

8. Use reduce+includes

function unique(arr){
    return arr.reduce((prev,cur) = > prev.includes(cur) ? prev : [...prev,cur],[]);
}

let arr = [1.0.0.2.4.4.8.3.1];
console.log(unique(arr));   / /,0,2,4,8,3 [1]
Copy the code

Use a double for loop and reuse the array splice method.

var arr = [1.5.6.0.7.3.0.5.9.5.5];
function unique(arr) {
    for (var i = 0, len = arr.length; i < len; i++) {
        for (var j = i + 1, len = arr.length; j < len; j++) {
            if (arr[i] === arr[j]) {
                arr.splice(j, 1);
                j--;        // The value of j decreases by 1 for every number removed
                len--;      // Len should be reduced by 1 when j is reduced.
                // console.log(j,len)}}}return arr;
}
console.log(unique(arr));       // 1, 5, 6, 0, 7, 3, 9
Copy the code

Use functions to recurse the weight

var arr = [1.1.5.6.0.9.3.0.6]
function unique( arr ){
  var arr1 = arr;
   var len = arr1.length;
   arr1.sort((a,b) = >{
       return a-b
   })
   function loop(index){
       if(index >= 1) {if(arr1[index] === arr1[index-1] ){
              arr1.splice(index,1);
         }
          loop(index - 1);  // loop recursively, then the array is deduplicated
     }
 }
 loop(len-1);   
  return arr1
}
console.log(unique(arr));    // 0, 1, 3, 5, 6, 9
Copy the code

11, using the attributes of the object can not be the same feature to duplicate (not recommended, defective)

Note: Using an array item as an object property automatically removes the same value

var arr = [1.3.8.9.3.5.4.4.6.6.2];
function unique(arr){
    var arr1 = [];
    var obj = {};
    for(var i =0,len=arr.length; i<len; i++){if(! obj[arr[i]]){ arr1.push(arr[i]); obj[arr[i]] =1;
        }else{ obj[arr[i]]++; }}return arr1;
}
console.log(unique(arr));   // 1, 3, 8, 9, 5, 4, 6, 2
Copy the code