// By ClayIdols
//生成器
Array.prototype.uniq = function () {
var hasNaN = false;
for(i=0;i<this.length;i++){
for(j=i-1;j>=0;j--){
if((this[i] === this[j]) || (hasNaN && this[i]!==this[i])){
this.splice(i,1);
}
}
if(!hasNaN && this[i]!==this[i]){
hasNaN = true;
}
}
}
x = [false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN];
console.log(x);
x.uniq();
console.log(x);
Array.prototype.uniq = function () {
let arr = this, length = this.length, hash = {}, res = []
for (let i = 0; i < length; i++) {
if (!hash[arr[i]] || /Object/.test(Object.prototype.toString.call(arr[i]))) {
res.push(arr[i])
hash[arr[i]] = 1
}
}
return res
}
/***
*
* ES6 使用集合直接进行去重操作
function unique(arr){
return [...new Set(arr)];
}
*/
Array.prototype.uniq = function () {
//1. 定义一个数组 和 标志位
let finalArr = [];
let flag = true;
// for循环操作
for (let i = 0; i < this.length; i++) {
// 使用indexOf方法 如果要检索的字符串值没有出现,则该方法返回 -1
if (finalArr.indexOf(this[i]) == -1) {
if (this[i] != this[i]) { //排除 NaN
if (flag) {
// 加入去重数组中
finalArr.push(this[i]);
// 改变标志位
flag = false;
}
} else {
//直接进行 push操作
finalArr.push(this[i]);
}
}
}
return finalArr;
} // 为什么这样写不通过
Array.prototype.uniq = function() {
return this.filter(function(ele,index,arr){
return index === arr.indexOf(ele);
});
};
Array.prototype.uniq = function () {
var len = this.length;
for (var i = 0; i < len; i++) {
for (var j = i + 1; j < len; j++) {
// 判断通常情况,{}==={}的判断只会判断引用地址是否一致,相同才会过滤掉
if (this[i] === this[j]) {
this.splice(j--, 1);
len--;
// 下一行语句判断NaN的情况
} else if (this[i] != this[i] && this[j] != this[j]) {
this.splice(j--, 1);
len--;
}
}
}
return this;
};
Array.prototype.uniq = function () {
var arr = [];
var flag = true;
this.forEach(function(item) {
// 排除 NaN (重要!!!)
if (item != item) {
flag && arr.indexOf(item) === -1 ? arr.push(item) : '';//push这个地方一定要与后面的冒号之间有空格否则会出错
flag = false;
} else {
arr.indexOf(item) === -1 ? arr.push(item) : ''
}
});
return arr;
}
//删除,改变数组,不用返回删除的数,测试数组中有NaN,要把重复的NaN删除
//一次循环
Array.prototype.uniq = function () {
var flag=true;
for(var i=0; i<this.length;){
if(this.indexOf(this[i]) !== i){ //包括重复的和NaN
if(this[i] !== this[i]){ //为NaN
if(flag){flag = !flag;//第一个NaN
i++;
} else{this.splice(i,1);}//重复的NaN
}else{this.splice(i,1);} //除NaN后重复的
}else{i++;}//不重复的
}
return this;
}
Array.prototype.uniq = function () {
var flag=true;
return this.filter(function(item,index,array){
if(item!=item&&flag){
array.splice(index,1,item);
flag=false;
return index !=array.indexOf(item);
}else{
return index ===array.indexOf(item);
}
});
};
Array.prototype.uniq = function () {
var obj = {},newArr = [],item = null,key = null;
for (var i = 0; i < this.length; i++) {
item = this[i];
var type = Object.prototype.toString.call(null).replace(/\[|\]/g,"").split(" ")[1];
var key = type+item;
// key = typeof this[i] + item;
//过滤出Object
if(!obj[key] || key.indexOf("object")>-1){
newArr.push(item);
obj[key] = true;
}
}
return newArr;
}
[false, true, undefined, null,null,new Date(),new Date(),NaN, 0, 1, {}, {}, 'a', 'a', NaN].uniq();
Array.prototype.uniq = function () {
var gotFirstNaN = false
var gotFirstEmptyObj = false
return this.filter(function(val,idx,arr){
// 处理 NaN 的情况
if (!val && isNaN(val) && val !== undefined && !gotFirstNaN ) {
gotFirstNaN = true
return true
}
// 处理 {} 的情况 (题目未要求,删除这段可AC)
else if (val instanceof Object && !val.getOwnPropertyNames){
if (!gotFirstEmptyObj) {
gotFirstEmptyObj = true
return true
}
return false
}
// 处理一般情况
return arr.indexOf(val) === idx
});
}
var a = [false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN].uniq()
console.log(a)
Array.prototype.uniq = function () {
var result=[];
var pushedNaN=false;
var pushednullobj=false;
for(var i =0;i<this.length;i++){
var index1=this.indexOf(this[i]);
var index2=this.lastIndexOf(this[i]);
if(index1!=index2){
result.push(this[i]);
while(index1!=this.lastIndexOf(this[i])){
this.splice(this.lastIndexOf(this[i]),1);
}
}else if(index1==-1){//针对NaN做的判断,只有第一次出现的NaN能够push进result
if(!pushedNaN){
result.push(this[i]);
pushedNaN=true;
}
}else if(this[i]!=this[i]){//针对{}做的判断,本地测试时没起作用,且如果是两个不同的对象,则这个地方所做处理不对。需要增加判断对象属性及值的机制。但是估计这个测试用例没考虑{}
if(!pushednullobj){
result.push(this[i]);
pushednullobj=true;
}
}else{
result.push(this[i]);
}
}
return result;
}
// recursion style
Array.prototype.uniq = function() {
function helper(plus, sub) {
if(sub.length === 0) return plus
else{
let p = plus.concat(sub[0])
let s = sub.filter((x) => {
if(isNaN(sub[0])&&typeof sub[0] === 'number'){
return !(isNaN(x)&&typeof x === 'number')
}else{
return sub[0] !== x
}
})
return helper(p, s)
}
}
return helper([], this)
}
var test1 = [false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN]
console.log(test1.uniq()) // [false, true, undefined, null, NaN, 0, 1, Object {...}, Object {...}, "a"]