deduplication(){let a =[1,1,2,3,5,6,4,2]let b =newSet(a)}
2.通过双层for循环暴力去重,效率不高
deduplication(){let a =[1,1,2,3,5,6,4,2]let flag =false//标志:当新数组中没有旧数组元素,设为true可插入let b =newArray()
b.push(a[0])for(let i =0;i<a.length;i++){for(let j =0;j<b.length;j++){if(b[j]== a[i]){
flag =falsebreak}
flag =true}if(flag ==true){
b.push(a[i])}}
console.log(b)},
3.先对数组进行sort()排序,再比较相邻的元素即可
deduplication(){let a =[1,1,2,3,5,6,1,4,2,1]let b = a.sort()//对a数组进行排序let c =newArray()
c.push(b[0])for(let i =1; i < b.length ;i++){if(b[i]!= b[i-1]){
c.push(b[i-1])}}
console.log(c)}
4.采用map结构来去重,map中有相同的键会更新,而不是添加
deduplication(){let a =[1,1,2,3,5,6,1,4,2]let map =newMap()let arr =newArray()for(let i =0; i<a.length; i++){if(map[a[i]]===undefined){
map[a[i]]= i
arr.push(a[i])}}return arr
},