// 第一种方法
var arr = [1,2,3,1,43,12,12,1],res=[],r=false;
function doNum(arr){
console.log(arr);
for (var i = 0;i<arr.length; i++){
if (res.length==0){
res.push(arr[i]);
}else{
r = false;
for (var m = 0;m < res.length;m++){
if (!r && res[m] == arr[i]){
r = true;
}
}
if(!r){
res.push(arr[i]);
}
}
}
console.log(res);
}
doNum(arr);
// 第二种方法
var arr1 = [1,2,3,1,43,12,12,1];
function doNumber(arr){
arr.sort(function(a,b){return a-b;});
for (var i = 0; i<arr.length; i++){
if (arr[i] == arr[i+1]){
arr.splice(i,1);
i--;
}
}
console.log('arr 结果是:'+arr);
}
doNumber(arr1);
// 第三种方法
var arr2 = [1,2,3,1,43,12,12,1];
function doNum1(arr){
var rec = {};
var result = [];
for(var i=0;i<arr.length;i++){
if (!rec[arr[i]]){
rec[arr[i]] = true;
result.push(arr[i]);
}
}
console.log(result);
}
doNum1(arr2);