Array to re belongs to the interview high frequency examination point, looked up a lot of blogs, saw the formidable blogger even listed 12 hours array to re method, but did not give a specific explanation of the array to re process, this is not friendly for new people, today, by me to help you open the door to the new world.

1. The ES6 to heavy

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

Set is a new Set data structure in ES6. It is similar to an array, but the values of its members can only be unique.

Advantages: Minimal code.

Disadvantages: Compatibility considerations, cannot remove {} objects

2. Double cycle weight removal

function unique(arr) {
for (let i = 0; i < arr.length; i++) {
  for (let j = i + 1; j < arr.length; j++) {
	if (arr[i] === arr[j]) {
		arr.splice(j, 1);
		j--;
	}
     }
} 
  return arr;
}
Copy the code

Double layer loop, outer layer loop element, inner layer loop when comparing values. If the values are the same, delete the element.

Pros: Easy to understand

Disadvantages: Time complexity is O(n^2), low efficiency

3. Use indexOf to remove weight

function unique(arr) {
let newArr = [];
for (let i = 0; i < arr.length; i++) {
      if (newArr.indexOf(arr[i]) === -1) {
        newArr.push(arr[i]);
     }
}
     return newArr;
}
Copy the code

Create an empty array, loop through the original array, and check whether the array contains the current element. If the array has the same value, skip it. If the array has different values, push it into the array.

The pros and cons are similar to the last one.

4. The filter to heavy

function unique(arr) {
   return arr.filter((temp, index) => arr.indexOf(temp) === index);
}
Copy the code

Idea: Take only the first occurrence of the element

Advantages: Less code.

Cons: a little hard to understand.

5. Reduce weight

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

Reduce iterates through an empty array to determine whether the following element has already appeared in the previous array. If so, return the previous array. Otherwise, add the new element to the array.

Advantages: Less code.

Cons: a little hard to understand.

6. Use Map to remove weight

function unique(arr) {
let map = new Map();
let newArr = [];
for (let i = 0; i < arr.length; i++) {
  if (map.has(arr[i])) {
     map.set(arr[i], true);
} else {
     map.set(arr[i], false);
     newArr.push(arr[i]);
    }
}
    return newArr;
}
Copy the code

A Map is similar to an Object, except that the key of an Object can only be a string, whereas the key of a Map can be any data type.

Create an empty Map data structure that iterates through the array and stores 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

Advantages: This de-duplication can be extended to other topics, such as what are the recurring prototypes and how many of them occur.

Faults: feels like no faults, just can’t seem to get rid of {} repetition.

7. Using hasOwnProperty

function unique(arr) {
const obj = {};
return arr.filter((item, index, array) => {
    return obj.hasOwnProperty(typeof item + JSON.stringify(item)) ? false : (obj[typeof item + JSON.stringify(item)] = true);
});
}
Copy the code

HasOwnProperty: hasOwnProperty: hasOwnProperty: hasOwnProperty: hasOwnProperty: hasOwnProperty: hasOwnProperty: hasOwnProperty: hasOwnProperty

I will also write a blog about ES6 syntax in the future, please pay attention to it. Thank you for seeing this.