js实现复杂度为N的数组去重
function unique(arr) {
let newarr = []
let hash = []
let index = 0
for (var i = 0; i < arr.length; i++) {
//采用hash的思想 取原数组的元素作为哈希值并赋值为 1(可赋任意值)
if (!hash[arr[i]]) {
newarr[index++] = arr[i]
hash[arr[i]]=1
}
}
return newarr
}
//测试数据
var arr=[1,2,3,5,1,1,2,2,45,1,45,6]
console.log(unique(arr))//[1,2,3,5,45,6]