统计、去重,找出重复,出现次数最多的字母
1、找到重复的元素
只找到是哪几个重复的元素
const findDup = (list = []) => { const result = []; list.forEach(item => { if(list.indexOf(item) !== list.lastIndexOf(item) && !result.includes(item)){ result.push(item); } }) return result; }
统计
找到重复的元素,并得到有几个
1、使用reduce方法,reduce本来就有统计的含义
const findDupsNumber = (list = []) => { return list.reduce((total, cur) => { if(cur in total){ total[cur]++ } else{ total[cur] = 1 } return total;
}, {}) }
数组去重
1/直接遍历,结果数组没有,存入结果数组中。
const deleDup = (list = []) => { const resultList = []; list.forEach(item=> { if(!resultList.includes(item)){ resultList.push(item) } }) return resultList; }
2、用reduce的方法
const deleDup = (list = []) => list.reduce((total, cur) => { if (!total.includes(cur)) { total.push(cur); } return total; }, []);