There is an array [1, 3, 5, 7, 9, 1, 5, 9]

With a certain algorithm, get the array [1, 3, 5, 7, 9]

Through the array

    let arr = [1.3.5.7.9.1.3.5]

    function arrUnique(arr) {
      let result = []
      for (let k of arr) {
        if(! result.includes(k)) { result.push(k) } }return result
    }
    console.log( arrUnique(arr) )
Copy the code

Use object key-value pairs

    let arr = [1.3.5.7.9.1.3.5]

    function arrUnique(arr) {
      let result = []
      let obj = {}
      for (let i = 0; i < arr.length; i++) {
        let val = arr[i]
        if(! obj[val]) { obj[val] =true
          result.push(arr[i])
        }
      }
      return result
    }
    console.log(arrUnique(arr))
Copy the code

Sort first, then undo

    let arr = [1.3.5.7.9.1.3.5]

    function arrUnique(arr) {
      let result = [arr[0]]
      arr.sort((a, b) = > a - b)
      for (let i = 0; i < arr.length; i++) {
        if(arr[i] ! == result[result.length -1]) {
          result.push(arr[i])
        }
      }

      return result
    }
    console.log(arrUnique(arr))
Copy the code

Use ES6’s Set data structure

    let arr = [1.3.5.7.9.1.3.5]

    function arrUnique(arr) {
      return [...new Set(arr)]
    }
    console.log(arrUnique(arr))
Copy the code