当前位置:网站首页>数组去重。。。。
数组去重。。。。
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;
}
结束。。。
边栏推荐
- Record the first data preprocessing process
- Verilog realizes binocular camera image data acquisition and Modelsim simulation, and finally matlab performs image display
- Warning: Each child in a list should have a unique “key“ prop.
- Docker installation of MySQL and redis
- Shandong University machine learning final 2021
- Autojs, read one line, delete one line, and stop scripts other than your own
- 解决ffmpeg獲取AAC音頻文件duration不准
- 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
- Examinelistactivity of Shandong University project training
- Review Servlet
猜你喜欢

Matlab实现均值滤波与FPGA进行对比,并采用modelsim波形仿真

Sqli-libs post injection question 11-17 actual combat

A piece of code has been refactored six times by the boss, and my mind is broken

FPGA面试题目笔记(三)——跨时钟域中握手信号同步的实现、任意分频、进制转换、RAM存储器等、原码反码和补码

FPGA interview notes (II) -- synchronous asynchronous D flip-flop, static and dynamic timing analysis, frequency division design, retiming

关于SIoU的原理和代码实现(回顾IoU、GIoU、DIoU、CIoU)

不同VLAN间的通信

Global case | how an airline with a history of 100 years can expand and transform to promote innovation in the aviation industry

Résoudre le problème de la durée inexacte du fichier audio AAC obtenu par ffmpeg

Detailed installation instructions for MySQL
随机推荐
个人常用软件及浏览器插件分享
Ethical discussion on reptile Technology
Warning: Each child in a list should have a unique “key“ prop.
jenkins-凭证管理
C language war "minesweeping"
Don't be afraid of xxE vulnerabilities: understand their ferocity and detection methods
Box model
Sqli-libs range 23-24 filtration and secondary injection practice
MATLAB realizes mean filtering and FPGA for comparison, and uses Modelsim waveform simulation
On the social moral and ethical issues behind short videos (personal point of view, for reference only)
QT socket setting connection timeout
Eureka cluster setup
Stock K-line drawing
call和apply和bind的区别
关于SIoU的原理和代码实现(回顾IoU、GIoU、DIoU、CIoU)
Use of constructors
Wechat applet (authorized login of TP5)
[TP5 online export picture generation excel detailed explanation example]
Handwriting promise [03] - realize multiple calls and chain calls of then method
How to use perforce helix core with CI build server