找出数组中重复出现的项
2021-02-26 Javascript
找出数组中重复出现的项
数组中元素为字符串 #
function random(){
return Math.floor(Math.random()*100)
}
let array = Array(1000).fill(1).map(i=>random())
console.time()
let [_,r] = array.reduce((pre,current,index)=>{
const [list, repeat]=pre;
if(list.includes(current)){
repeat[current].push(index)
}else{
repeat[current]=[index]
}
list.push(current)
return [list,repeat]
},[[],{}])
let p = Object.entries(r).filter(item=> item[1].length>1)
console.timeEnd()
function random(){
return Math.floor(Math.random()*100)
}
let array = Array(1000).fill(1).map(i=>random())
console.time()
let [_,r] = array.reduce((pre,current,index)=>{
const [list, repeat]=pre;
if(list.includes(current)){
repeat[current].push(index)
}else{
repeat[current]=[index]
}
list.push(current)
return [list,repeat]
},[[],{}])
let p = Object.entries(r).filter(item=> item[1].length>1)
console.timeEnd()
使用生成随机数,用来测试字符串的, 用reduce的性能比较好。
数组长度为1000000,查找时间为 193ms。
数组中的元素为对象 #
let arr = [
{name:'小明',age:12,height:88},
{name:'小明2',age:12,height:58},
{name:'小明4',age:12,height:28},
{name:'小明',age:12,height:88},
{name:'小明',age:12,height:88},
{name:'小红',age:12,height:88},
{name:'小红',age:12,height:88},
{name:'小黄',age:12,height:88},
{name:'小黄',age:12,height:88},
{name:'小红',age:12,height:88},
]
let b = arr.reduce((pre, next, index)=>{
let name = next.name
if(!pre.hasOwnProperty(name)){
pre[name] = []
}
pre[name].push(index)
return pre
},{})
let r = Object.entries(b).filter(item=> item[1].length> 1)
let arr = [
{name:'小明',age:12,height:88},
{name:'小明2',age:12,height:58},
{name:'小明4',age:12,height:28},
{name:'小明',age:12,height:88},
{name:'小明',age:12,height:88},
{name:'小红',age:12,height:88},
{name:'小红',age:12,height:88},
{name:'小黄',age:12,height:88},
{name:'小黄',age:12,height:88},
{name:'小红',age:12,height:88},
]
let b = arr.reduce((pre, next, index)=>{
let name = next.name
if(!pre.hasOwnProperty(name)){
pre[name] = []
}
pre[name].push(index)
return pre
},{})
let r = Object.entries(b).filter(item=> item[1].length> 1)
找出字符串中出现连续重复的字符 #
// *记录连续重复的字符并输出
let s = 'sss11sa22aewqsdasxxssewfdsfwwdgseedffwww';
function search(str) {
if (str.length === 0) {
return
}
let prev = ''
let result = []
let current = ''
for (let index = 0; index < str.length; index++) {
const element = str[index];
if (prev === element) {
current = current + element
} else {
if (current.length) {
current += prev
result.push(current);
current = ''
}
prev = element;
}
}
return result;
}
console.log(search(s))
// *记录连续重复的字符并输出
let s = 'sss11sa22aewqsdasxxssewfdsfwwdgseedffwww';
function search(str) {
if (str.length === 0) {
return
}
let prev = ''
let result = []
let current = ''
for (let index = 0; index < str.length; index++) {
const element = str[index];
if (prev === element) {
current = current + element
} else {
if (current.length) {
current += prev
result.push(current);
current = ''
}
prev = element;
}
}
return result;
}
console.log(search(s))
符出现的多次的记录 #
let a2 = ['s','y','s','s','d','3','y']
let record = {}
for(let i = 0;i< a2.length; i++){
let key = a2[i]
if(record[key]){
record[key].push(i)
}else{
record[key] = [i]
}
}
let recordArray = Object.entries(record).filter((value)=>{
if(value[1].length>1){
return {[value[0]]: value[1]}
}
})
console.log('recordArray',recordArray)
// recordArray [ [ 's', [ 0, 2, 3 ] ], [ 'y', [ 1, 6 ] ] ]
let a2 = ['s','y','s','s','d','3','y']
let record = {}
for(let i = 0;i< a2.length; i++){
let key = a2[i]
if(record[key]){
record[key].push(i)
}else{
record[key] = [i]
}
}
let recordArray = Object.entries(record).filter((value)=>{
if(value[1].length>1){
return {[value[0]]: value[1]}
}
})
console.log('recordArray',recordArray)
// recordArray [ [ 's', [ 0, 2, 3 ] ], [ 'y', [ 1, 6 ] ] ]
更优方案 #
let s = 'sss11sa22aewqsdasxxssewfdsfwwdgseedffwww';
function search(str) {
if (str.length === 0) {
return
}
let prev = ''
let result = []
let current = ''
for (let index = 0; index < str.length; index++) {
const element = str[index];
if (prev === element) {
current = current + element
} else {
if (current.length) {
current += prev
result.push(current);
current = ''
}
prev = element;
}
}
console.log(result)
return result;
}
let s = 'sss11sa22aewqsdasxxssewfdsfwwdgseedffwww';
function search(str) {
if (str.length === 0) {
return
}
let prev = ''
let result = []
let current = ''
for (let index = 0; index < str.length; index++) {
const element = str[index];
if (prev === element) {
current = current + element
} else {
if (current.length) {
current += prev
result.push(current);
current = ''
}
prev = element;
}
}
console.log(result)
return result;
}
版权属于: vincent
转载时须注明出处及本声明
Tags:# Javascript