当前位置:网站首页>Number array de duplication in JS
Number array de duplication in JS
2022-06-25 23:56:00 【wen_ Wen Wen】
// Method 1: ES6 Medium Set Data structure method
function arrRemoval(array) {
// let set = Array.from(new Set(array));
let set = [...new Set(array)];
return set;
}
// Method 2: Define an empty new array , Each time, judge whether the new array contains the current element , If not, add
function arrRemoval(array) {
let result = [];
array.forEach((ele,ind) =>{
if(!result.includes(ele)) {
result.push(ele);
}
})
return result;
}
// Method 3: indexOf Law , indexof(item): Returns the index of the first element found in the array
function arrRemoval(array) {
let result = [];
result = array.filter((ele,ind) =>{
return array.indexOf(ele)===ind;
})
return result;
}
// Method 4: reduce Law :
function arrRemoval(array) {
let result = [];
// Define the initial pre by []
result = array.reduce( function(pre, item){
// return pre.indexOf(item)!==-1 ? pre : pre.concat(item);
return pre.indexOf(item)!==-1 ? pre : [...pre,item];
}, [])
return result;
}
// Method 5: Leverage the key Non repeatable features , If key Repetition is set later value Cover the front of value
function arrRemoval(array) {
let result = {};
// Define the initial pre by []
array.forEach((ele,ind) => {
result[ele] = ind;
});
// console.log(Object.keys(result)); //["1", "2", "3", "4", "5"]
return Object.keys(result).map((ele,ind)=>{
// ~~ The returned result is numeric
// return ~~ele;
return parseInt(ele);
})
}
// Method 6: Prioritize , Then compare the i Xiang He i+1 term ,
function arrRemoval(array) {
let result = [];
array.sort();
for(let i=0;i<array.length;i++) {
if(array[i] !== array[i+1]) {
result.push(array[i]);
}
}
return result;
}
// Method 7: Compare the current element with each item after the array , If there are equality items, discard the current element , If there is no equality, it will be added to the new array
function arrRemoval(array) {
let result = [];
for(let i=0;i<array.length;i++) {
for(let j=i+1;j<array.length;j++) {
if(array[i] == array[j]) {
i++;
}
}
result.push(array[i]);
}
return result;
}
边栏推荐
- Understanding of pseudo classes
- IDEA中如何生成get/set方法
- Alipay payment interface sandbox environment test and integration into an SSM e-commerce project
- mysql集群
- Object类常用方法
- Doris 运维中遇到的问题
- IDEA常用快捷键
- C# IO Stream 流(一)基础概念_基本定义
- The InputStream stream has been closed, but the file or folder cannot be deleted, indicating that it is occupied by the JVM
- PHP interprocess pass file descriptor
猜你喜欢
如何进行流程创新,以最经济的方式提升产品体验?
Unable to start debugging. Unexpected GDB output from command “-environment -cd xxx“ No such file or
C ++ 引用与指针总结
idea Kotlin版本升级
STEP7主站与远程I/O组网_过路老熊_新浪博客
《网络是怎么样连接的》读书笔记 - 集线器、路由器和路由器(三)
SSM整合学习笔记(主要是思路)
C# IO Stream 流(一)基础概念_基本定义
Simulation connection between WinCC and STEP7_ Old bear passing by_ Sina blog
解析产品开发失败的5个根本原因
随机推荐
Px4 multi computer simulation (gazebo)
DPVS-FullNAT模式部署篇
关于scrapy爬虫时,由spider文件将item传递到管道的方法注意事项
xtrabackup的备份还原
Mutual conversion of float type data and datetime type data in sqlserver2008
SSM整合学习笔记(主要是思路)
Topic36——53. 最大子数组和
Stop eating vitamin C tablets. These six fruits have the highest vitamin C content
手工制作 pl-2303hx 的USB轉TTL電平串口的電路_過路老熊_新浪博客
Unsigned and signed vernacular
关于Swoole协程容器
Connecting MySQL database with VBScript_ Old bear passing by_ Sina blog
实例:用C#.NET手把手教你做微信公众号开发(21)--使用微信支付线上收款:H5方式
Talk about singleton mode!
权限设计=功能权限+数据权限
SSM integrated learning notes (mainly ideas)
说说单例模式!
Virtual and pure virtual destructors and their implementation in c++
C# IO Stream 流(二)扩展类_封装器
【微信公众号H5】 生成带参数进入公众号关注页的二维码 监听用户关注公众号事件 自定义菜单栏 (服务端)