方法一:纯数字数组、纯字母数组、纯中文数组
Array.prototype.distinct = function() {
var ret = [] ;
for(var i = 0;i<this.length;i++){
for(var j = i+1;j<this.length;){
if(this[i] == this[j]){
this.splice(j,1);
}else{
j++
}
}
}
for(var i = this.length-1;i>0;i--){
for(var j = 0;j<i;j++){
if(this[j] > this[j+1]){
var temp = this[j] ;
this[j] = this[j+1];
this[j+1] = temp ;
}
}
}
ret = this ;
return ret ;
}
var arr1 = ["c", "b", "a", "d", "e", "a", "b"].distinct();
var arr2 = [11,3,45,23,2,56,9].distinct();
console.log(arr1);
console.log(arr2);
方法二:
如果已知数组的每一项都是数字:
var arr=[56,1,2,2,3,3,5,5,1,2,4,7,8,96,3,5,2,1,4,5];
//解析步骤
//1.把数组转换成一个JS对象
//2.把数组中的值变成JS对象中的Key
//