1、數組遍歷法數組
var arr=[10,2,2,3,4,5,3]ui
var newArr=[];spa
for(var i=0;i<arr.length;i++){排序
if(newarr.indexof(arr[i]===-1){token
newArr[i].push(arr[i])io
}console
}function
2、數組下標判斷法class
var arr = [2, 8, 5, 0, 5, 2, 6, 7, 2] 遍歷
var newArr = []
for (var i = 0; i < arr.length; i++)
{ if (arr.indexOf(arr[i]) === i) {
newArr.push(arr[i])
}
}
3、排序後相鄰去除法
var arr = [2, 8, 5, 0, 5, 2, 6, 7, 2]
arr.sort()
var newArr = [arr[0]]
for (var i = 1; i < arr.length; i++) {
if (arr[i] !== newArr[newArr.length - 1]) {
newArr.push(arr[i])
}
}
console.log(newArr)