1. Two-layer cycle: the one that occupies more memory and has the lowest efficiency

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

2. Filter + indexOf: Clean code but low performance

function deduplication(arr) {
  return arr.filter((item, index) => arr.indexOf(item) === index)
}
Copy the code

3. Layer 1 loop + sort: performance is ok

function deduplication(arr) { arr = arr.sort() let newArr = [arr[0]] for (let i=1, len=arr.length; i<len; i++) { arr[i] ! == arr[i-1] && newArr.push(arr[i]) } return newArr }Copy the code

4. Set: High performance and simplicity

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

5. Layer 1 loop + Object: the fastest speed

function deduplication(arr) { arr = arr.sort() let newArr = [] let obj = {} for (let i of arr) { if (! obj[i]) { newArr.push(i) obj[i] = 1 } } return newArr }Copy the code