首页 > 试题广场 >

数组去重

[编程题]数组去重
  • 热度指数:79453 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
为 Array 对象添加一个去除重复项的方法
示例1

输入

[false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN]

输出

[false, true, undefined, null, NaN, 0, 1, {}, {}, 'a']
Array.prototype.uniq = function () {
    return [...new Set(this)]
}
发表于 2023-06-28 16:36:18 回复(0)
Array.prototype.uniq = function () {
    const set1=new Set(this)
    let arr1=[...set1]
    return arr1
}
发表于 2023-05-11 17:35:22 回复(0)
Array.prototype.uniq = function () {
    return Array.from(new Set(this))
}
发表于 2023-02-08 18:01:02 回复(0)
Array.prototype.uniq = function () {
    return [...new Set(this)]
}

发表于 2022-11-26 00:04:04 回复(0)
//API战士
Array.prototype.uniq = function () {
    const set = new Set(this)
    return Array.from(set)
}
发表于 2022-11-21 14:21:01 回复(0)
Set方法执行后是一个类数组,转化为数组再输出
Array.prototype.uniq = function () {
    return [...new Set(this)]
}

发表于 2022-11-13 10:46:48 回复(0)
Array.prototype.uniq = function () {
   return  Array.from(new Set(this))
}
发表于 2022-10-22 14:48:57 回复(0)
Array.prototype.uniq = function () {
    let arr = this;
    let newArr = [];
    let oNum = 0;
    let nNum = 0;
    arr.forEach(item => {
        if(newArr.length == 0){
            newArr.push(item);
        }
        let type = typeof item;
        //判断除NaN,{},null,数字之外元素
        if(type!="object"&&type!="number"&&newArr.indexOf(item)<0){
            newArr.push(item);
        }
        //判断{},null
        if(type=="object"){
            if(newArr.indexOf(item)<0&&item!=null){
                newArr.push(item);
            }else if(oNum<1){
                oNum++;
                newArr.push(item);
            }
        }
        //判断NaN,数字
        if(type=='number'){
            if(isNaN(item)&&nNum==0){
                nNum++;
                newArr.push(NaN);
            }
            if(!isNaN(item)&&newArr.indexOf(item)<0){
                newArr.push(item);
            }
        }
    })
    return newArr;
}
发表于 2022-10-14 14:05:55 回复(0)
// 使用 Array.prototype.includes() 可避免判断NaN
Array.prototype.uniq = function () {
    var arr = []
    for(var i=0; i<this.length; i++){
        if(!arr.includes(this[i])){
            arr.push(this[i])
        }
    }
    return arr
}
发表于 2022-10-03 16:43:38 回复(0)
Array.prototype.uniq = function () {
    let arr = this;
    return Array.from(new Set(arr));
}

发表于 2022-09-30 17:01:58 回复(0)
Array.prototype.uniq = function() {
                let arr = this
                let newarr = []
                for (i = 0; i < arr1.length; i++) {
                    if (newarr.includes(arr[i])==false) {
                        newarr.push(arr[i])
                    }else{
                        
                    }
                }
                return newarr
            }
            let arr1 = [false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN]
            let arr2 = arr1.uniq()
            console.log(arr2);
这个浏览器运行没错呀 牛客过不了
发表于 2022-08-05 17:27:25 回复(0)
Array.prototype.uniq = function () {
    return Array.from(new Set(this))
}

发表于 2022-07-17 23:50:46 回复(0)
Array.prototype.uniq = function () {
    // 1.使用set的特点进行去重,如果this指向比较复杂可以在外面定义一个that接受this
    return Array.from(new Set(this))
     
    // 2.使用循环,定义一个数组接受未存在的值,重复的值不会存进去
    let newArr = []
    this.forEach(item=>{
        if(!newArr.includes(item)) newArr.push(item)
    })
    return newArr
}

发表于 2022-07-06 13:56:51 回复(0)
方法1:
Array.prototype.uniq = function () {
    return this.reduce((pre,cur)=>pre.includes(cur)?pre:[...pre,cur],[]);
}
方法2:
Array.prototype.uniq = function () {
    return [...new Set(this)];
}
方法3:
Array.prototype.uniq = function () {
   var resArr = [];
   var flag = true;
     
   for(var i=0;i<this.length;i++){
       if(resArr.indexOf(this[i]) == -1){
           if(this[i] != this[i]){   //排除 NaN
              if(flag){
                   resArr.push(this[i]);
                   flag = false;
              }
           }else{
                resArr.push(this[i]);
           }
       }
   }
    return resArr;
}
方法4:
Array.prototype.uniq = function () {
    var arr = [];
    for(var i = 0; i < this.length; i++) {
        if(!arr.includes(this[i])) {
            arr.push(this[i]);
        }
    }
    return arr;
}





发表于 2022-06-23 13:40:01 回复(0)
Array.prototype.uniq = function () {
    return this.reduce(function(prev,item){
        if(prev.includes(item)){
            return prev
        }else{
           return prev.concat(item)
        }
    },[])
}
发表于 2022-06-21 12:31:53 回复(0)
Array.prototype.uniq = function () {
    let hasNaN = false;
    for(let i = this.length - 1; i >= 0; i--){
        if(this[i] != this[i]){
            if(!hasNaN) {
                hasNaN = true;
            } else {
                this.splice(i, 1);
            }
        } else if(this.indexOf(this[i]) !== i) {
            this.splice(i, 1);
        }
    }
    return this;
}
// [true, false, null, undefined, 0, 1, {…}, {…}, 'a', NaN] 
这个NaN的位置还可能在最后,这个校验没有覆盖住
发表于 2022-06-07 13:48:04 回复(0)
function bindThis(f, oTarget) {
    return function() {
        return f.apply(oTarget, arguments);
    }
}

Array.prototype.uniq = function () {
    return [...new Set(this)];
}

发表于 2022-03-26 16:55:51 回复(0)
Array.prototype.uniq = function () {
     let res = [];
     flag = 0;
     this.forEach(item => {
         console.log(item);
         if(item != item) {
             if(flag < 1) res.push(item);
             flag ++;  
         }
         if(res.indexOf(item) == -1 && item == item) res.push(item);
         
     })
     return res;
 }
需要单独判断数组中的NaN,因为使用indexOf判断,即使数组中含有NaN,也会返回-1,这回导致去重后的数组仍然含有原数组数目的NaN。
借助NaN != NaN的特性,可以快速判断一个元素是否为NaN。


也可以使用Set,简单方便,不需要自己考虑NaN的问题
Array.prototype.uniq = function () {
    return [...new Set(this)];
}


发表于 2022-03-13 18:03:00 回复(0)

问题信息

难度:
49条回答 21697浏览

热门推荐

通过挑战的用户

查看代码
数组去重