首页 > 试题广场 >

数组去重

[编程题]数组去重
  • 热度指数:28125 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
请补全JavaScript代码,要求去除数组参数中的重复数字项并返回该数组。
注意:
1. 数组参数仅包含数字
示例1

输入

_deleteRepeat([-1,1,2,2])

输出

[-1,1,2]
const _deleteRepeat = array => {
// 补全代码
var a={}
for(let i=0;i<array.length;i++){
a[array[i]]=0
console.log(a)
}
return Object.values(a)
}
console.log(_deleteRepeat([1,2,1,3,4,1,-1,-1,2,1]))

这里的测试不知道为什么总是错,但实际上目前的各种ide编译都是通过的
发表于 2023-06-16 20:22:03 回复(0)
用Set去重:
<!DOCTYPE html>
<html>
    <head>
        <meta charset=utf-8>
    </head>
    <body>
    	
        <script type="text/javascript">
            const _deleteRepeat = array => {
                // 补全代码
                let ans = [];
                (new Set(array)).forEach((v1, v2, s) => ans.push(v1))
                return ans;
            }
        </script>
    </body>
</html>


发表于 2023-05-27 19:52:34 回复(0)
          const _deleteRepeat=array => Array.from(new Set(array))
          console.log(_deleteRepeat([-1,1,2,2]))

发表于 2023-04-16 16:28:05 回复(0)
            const _deleteRepeat = array => {
                let set = new Set(array)
                let res = Array.from(set)
                return res
            }
发表于 2023-04-06 15:29:24 回复(0)
 return [...new Set(array)]; 
发表于 2023-02-16 19:59:36 回复(0)
直接
    return Array.from(new Set(array));
一行代码解决。
发表于 2023-02-01 12:42:23 回复(0)
const _deleteRepeat = array => {
                // 补全代码
                if (array.length <= 1) return array;
               
                const res = [array[0]];
                for (let i =1; i < array.length; i++) {
                    if (res.indexOf(array[i]) < 0) {
                        res.push(array[i]);
                    }
                }
                return res;
            }
发表于 2022-12-11 21:46:04 回复(0)
const _deleteRepeat = array => {
    // 补全代码
    return array.filter((el,index) => {
        return array.findIndex(item => el == item) == index
    })
}
发表于 2022-11-29 17:28:22 回复(0)
    1 数组去重, 定义一个空数组,配合indexOf,遍历循环
 <script type="text/javascript">
            const _deleteRepeat = array => {
                let res = []               
                for(let i = 0;i<array.length;i++){
                    if(res.indexOf(array[i]) === -1) {
                        res.push(array[i])
                    }
}

return res
                
            }
        </script>

发表于 2022-11-22 15:11:56 回复(0)
return [...new Set(array)]
发表于 2022-10-25 13:28:42 回复(0)
        <script type="text/javascript">
            const _deleteRepeat = array => {
                // 补全代码
                var newArray=[];
                for(var item of array){
                    if(newArray.indexOf(item)==-1){
                        newArray.push(item);
                    }
                }
                return newArray;
            }
        </script>

发表于 2022-09-17 16:18:46 回复(0)
                    for (var i=0; i<array.length; i++) {
                    let flag = array[i]
                    for (var j=i+1; j<array.length; j++) {
                        if (array[j] === flag) {
                            array.splice(j,1)
                        }
                    }
                }
                return array
发表于 2022-09-08 19:20:41 回复(0)
const _deleteRepeat = array => {
    // 补全代码
    let obj={}
    for(let i of array){
       obj[i]=i
    }
    return Object.values(obj)
}
这种为什么实际输出结果是null
发表于 2022-08-29 17:44:02 回复(0)
<!DOCTYPE html>
<html>
    <head>
        <meta charset=utf-8>
    </head>
    <body>
    	
        <script type="text/javascript">
            const _deleteRepeat = array => {
                // 补全代码
                let set = new Set(array)
                return [...set]
            }
        </script>
    </body>
</html>

发表于 2022-08-18 23:11:59 回复(0)
const _deleteRepeat = array => {
                // 补全代码
                let newArr = [];
                array.forEach(item => {
                    if (!newArr.includes(item)) {
                        newArr = [...newArr, item]
                    }
                })
                return newArr;
            }
发表于 2022-07-29 17:19:06 回复(0)
const _deleteRepeat = array => {
                // 补全代码
                let arr = []
                if(array.length !== 0){
                    array.map(item=>{
                        if(!arr.includes(item)){
                            arr.push(item)
                        }
                    })
                }
                return arr;
            }

发表于 2022-06-20 23:00:47 回复(0)
const _deleteRepeat = array => {
                // 补全代码
                let hash={}
                for(let i=0; i<=array.length; i++){
                    if(!hash[array[i]]){
                        hash[array[i]]=1
                    }else{
                        array.splice(i, 1);
                    }
                }
                return array
            }
            _deleteRepeat([-1,1,2,2])
这种方式为什么不可以呀!电脑后台运行符合要求:返回原数组+去重
发表于 2022-05-28 23:11:08 回复(0)
数组去重可以有五种写法。如下
//-----------------1. ES6的set方法
const _deleteRepeat = array => {
  return Array.from(new Set(array))
}
//-----------------2. 利用indexOf
const _deleteRepeat = array => {
  let res =[]
  array.forEach(item=>{
    if(res.indexOf(item)=== -1) res.push(item)
  })
  return res
}
//-----------------3. 利用includes
const _deleteRepeat = array => {
  let res =[]
  array.forEach(item=>{
    if(!res.includes(item)) res.push(item)
  })
  return res
}
//-----------------4. 利用filter,filter返回符合条件的元素组成的数组。
const _deleteRepeat = array => {
  return array.filter((item,index)=>{
    return array.indexOf(item)===index
  })
}
//-----------------5. 算法:双重for循环
const _deleteRepeat = array => {
  let len =array.length
  for(let i=0;i<len;i++){
    for(let j=i+1;j<len;j++){
      if(array[i]===array[j]){
        array.splice(j,1) //在j的位置删除一个元素,即删除j
        j--  //因为删除了一个元素,所以j要往前一位
        len--  //减少循环次数,提高效率
      }
    }
  }
  return array
}


发表于 2022-05-19 14:00:14 回复(0)
提供一个比较初级的法子,核心点在于用空数组去indexOf原数组,查不到就push进空数组。
const _deleteRepeat = array => {
                // 补全代码
                let b =[];
                for(var i = 0;i <array.length;i++){
                    if(b.indexOf(array[i]) != -1){

                    }else{
                        b.push(array[i])
                    }
                };
                console.log(b);
            };
            _deleteRepeat([-1,1,2,2]);
发表于 2022-05-16 14:55:24 回复(0)