Wolf Wolf dog, remember the first interview was asked the array to duplicate the implementation method. Here are some common methods.

Using hash to find

function unique(arr){
    var result =[],hash = {};
    for (var i = 0,len = arr.length; i<len; i++){
        if(!hash[arr[i]]){
            hash[arr[i]] = true;
            result.push(arr[i])
        }
    }
    return result;
}
Copy the code

Using the indexof

 function unique(arr){
    var result = []; 
    for(var i=0; i<arr.length; i++){
      if(arr.indexOf(arr[i]) == i){ result.push(arr[i]); }}return result;
  }
Copy the code
function unique(arr){
    return arr.filter(function (item, index, self) {
        return self.indexOf(item) === index;
    });
}
Copy the code

es6

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

function unique (arr) {
  const seen = new Map()
  returnarr.filter((a) => ! seen.has(a) && seen.set(a, 1)) }Copy the code

Please point out any inaccuracies