当前位置:网站首页>数组去重。。。。
数组去重。。。。
2022-06-11 06:23:00 【Saucey_6】
数组去重两种思路:
1.在原有数组基础上进行去重
2.申请新的数组进行去重操作
原有数组上进行操作:
- es6 set方法去重
function unique(arr){
return Array.from(new Set(arr))
}
splice直接去重
function unique2(arr){
for(let i=0;i<arr.length;i++){
for(let j=i+1;j<arr.length;j++){
if(arr[i]===arr[j]){
arr.splice(j,1)
j--
}
}
}
return arr
}
- 利用
hasOwnProperty判断是否存在对象属性
function unique5(arr) {
let obj={}
return arr.filter(function (item,index,arr) {
return obj.hasOwnProperty(typeof item + item) ? false : (obj[typeof item+item]=true)
})
}
- 利用好filter去判断当前元素是否等于原始数组中出现的第一个索引值
function unique6(arr) {
return arr.filter(function (item,index,arr) {
return arr.indexOf(item,0)===index
})
}
申请新的数组去重操作
- indexOf判断
function unique1(arr) {
if(!Array.isArray(arr)){
return 0
}
let result=[];
for(let i=0;i<arr.length;i++){
if(result.indexOf(arr[i])===-1){
result.push(arr[i])
}
}
return result
}
- 排序,相邻进行操作
function unique3(arr){
if(!Array.isArray(arr)){
return 0
}
arr.sort((a,b)=>{
return a-b
});
let result=[arr[0]]
for(let i=1;i<arr.length;i++){
if(arr[i]!==arr[i-1]){
result.push(arr[i])
}
}
return result
}
- 利用对象的属性不能相同的特点进行去重
function unique4(arr) {
if(!Array.isArray(arr)){
return 0
}
let result=[]
let obj={}
for(let i=0;i<arr.length;i++){
if(!obj[arr[i]]){
result.push(arr[i])
obj[arr[i]]=1
}else{
obj[arr[i]]++
}
}
return result
}
- map数据结构去重
function unique7(arr) {
let map=new Map()
let result=new Array();
for(let i=0;i<arr.length;i++){
if(map.has(arr[i])){
map.set(arr[i],true)
}else{
map.set(arr[i],false)
result.push(arr[i])
}
}
return result;
}
结束。。。
边栏推荐
- Graphsage paper reading
- FPGA面試題目筆記(四)—— 序列檢測器、跨時鐘域中的格雷碼、乒乓操作、降低靜動態損耗、定點化無損誤差、恢複時間和移除時間
- Make a small game with R language and only basic package
- Notes sur les questions d'entrevue de la FPGA (IV) - - détecteur de séquence, Code gris dans le domaine de l'horloge croisée, opération de ping - pong, réduction de la perte statique et dynamique, err
- Vulhub 8.1-backdoor vulnerability recurrence
- Chapter 4 of machine learning [series] naive Bayesian model
- FIFO最小深度计算的题目合集
- FPGA面试题目笔记(三)——跨时钟域中握手信号同步的实现、任意分频、进制转换、RAM存储器等、原码反码和补码
- On the social moral and ethical issues behind short videos (personal point of view, for reference only)
- Jenkins voucher management
猜你喜欢

Transfer Learning

Convert text label of dataset to digital label

Shandong University machine learning experiment VI k-means

563. 二叉树的坡度

The classification effect of converting video classification data set to picture classification data set on vgg16

FPGA面试题目笔记(四)—— 序列检测器、跨时钟域中的格雷码、乒乓操作、降低静动态损耗、定点化无损误差、恢复时间和移除时间

Sentinel annotation support - @sentinelresource usage details

Simple understanding of pseudo elements before and after

Using Metasploit Trojan horse for remote control

FPGA面试题目笔记(二)——同步异步D触发器、静动态时序分析、分频设计、Retiming
随机推荐
[reading this article is enough!!! Easy to understand] confidence level understanding (95% confidence level and confidence interval)
This point of arrow function
Difference between foreach, for... In and for... Of
ERROR 1215 (HY000): Cannot add foreign key constraint
Which company is better in JIRA organizational structure management?
Autojs, read one line, delete one line, and stop scripts other than your own
CCS安装编译器的方法
山东大学项目实训之examineListActivity
修复鼠标右键没有vscode快捷入口的问题
On the social moral and ethical issues behind short videos (personal point of view, for reference only)
Topic collection of FIFO minimum depth calculation
Warning: Each child in a list should have a unique “key“ prop.
Basic use of BufferedReader and bufferedwriter
021-MongoDB数据库从入门到放弃
Communication between different VLANs
Chapter 6 of machine learning [series] random forest model
Convert multiple pictures into one NPY file storage
Examinelistactivity of Shandong University project training
How to treat the ethical issues arising from driverless Technology
Wechat applet (authorized login of TP5)