当前位置:网站首页>查找字符串中重复次数最多的元素
查找字符串中重复次数最多的元素
2022-06-29 08:26:00 【swag_特约男演员】
思路(能力有限,比较菜,望多指导)
循环,第一重循环直接记count = 1,然后第二重循环在当前元素的后面找(注意:这里做了处理,不遍历重复的元素,如第一重循环的array[0]元素为s,第一重循环的array[5]元素仍为s则不进行下面的操作,因为已经遍历过了。),找到就count加1,把每一个元素和它重复的次数计入对象。
然后,遍历存放字符重复次数的对象,找出字符重复次数的最大值,再遍历一遍找到重复次数等于最大值的元素取出来。(这里不合并操作是因为,s重复次数为4,d重复次数也为4,这样会漏掉一个)
纯循环方法,复杂度飙升

借用正则表达式,复杂度也不低

源码
// 找出字符串中重复最多的字符并输出重复次数
function getMaxRepeatStr (String) {
let checked = [] // 检查过的元素
var stringArray = String.split('')
let obj = {
} // 存放字符重复次数的对象
for (var i = 0; i < stringArray.length; i++) {
if (!checked.includes(stringArray[i])) {
// 没检查过才进行二次循环
let count = 1 // 重复次数
obj[stringArray[i]] = count
for (var j = i + 1; j < stringArray.length; j++) {
if (stringArray[i] === stringArray[j]) {
count++
obj[stringArray[i]] = count
} else {
continue
}
}
checked.push(stringArray[i]) // 标记已检查
} else {
continue
}
}
// 找出重复次数最大值
let maxVal = 0
Object.keys(obj).forEach(element => {
if (obj[element] > maxVal) {
maxVal = obj[element]
}
})
// 取得重复次数最多的字符
let result = {
}
Object.keys(obj).forEach(element => {
if (obj[element] === maxVal) {
result[element] = obj[element]
}
})
console.log('字符串中重复次数最多的字符为:', result)
}
function getMaxRepeatStr (String) {
let checked = [] // 检查过的元素
var stringArray = String.split('')
let obj = {
} // 存放字符重复次数的对象
for (var i = 0; i < stringArray.length; i++) {
if (!checked.includes(stringArray[i])) {
// 没检查过才进行处理
let reg = new RegExp(stringArray[i], "ig");
let count = String.match(reg).length;
checked.push(stringArray[i]) // 标记已检查
obj[stringArray[i]] = count
} else {
continue
}
}
// 找出重复次数最大值
let maxVal = 0
Object.keys(obj).forEach(element => {
if (obj[element] > maxVal) {
maxVal = obj[element]
}
})
// 取得重复次数最多的字符
let result = {
}
Object.keys(obj).forEach(element => {
if (obj[element] === maxVal) {
result[element] = obj[element]
}
})
console.log('字符串中重复次数最多的字符为:', result)
}
getMaxRepeatStr('sdfjhkjhkjlsdsdfsd')
getMaxRepeatStr('dfghjjjklmnssss')
边栏推荐
猜你喜欢

laravel 8 实现 订单表按月份水平分表

ThreadLocal thread variable

Compare homekit, MI family, and zhiting family cloud edition for what scene based experiences

Verilog size and +: Using
Development tips - Image Resource Management

Wallpaper applet source code double ended wechat Tiktok applet

Huawei equipment is configured with small network WLAN basic services

TypeScript 变量声明 —— 类型断言

随心玩玩(三)Mirai框架QQ机器人

2022第六季完美童模 合肥赛区 决赛圆满落幕
随机推荐
Leetcode(142)——环形链表 II
js for in循环 for of循环的区别以及用法
The final of the sixth season of 2022 perfect children's model Hefei division came to a successful conclusion
Analysis of c voice endpoint detection (VAD) implementation process
Verilog expression
Leetcode (142) - circular linked list II
2022第六季完美童模 海口赛区 选拔赛圆满落幕
2022第六季完美童模 清远赛区 海选赛圆满落幕
Working for many years, recalling life -- three years in high school
Dialogue | prospects and challenges of privacy computing in the digital age
Operating system product key viewing method
机器人代码生成器之Robcogen使用教程
C# 语音端点检测(VAD)实现过程分析
Product manager certification enrollment brochure (NPDP) in July 2022
Pointnet的网络学习
[most complete] download and installation of various versions of PS and tutorial of small test ox knife (Photoshop CS3 ~ ~ Photoshop 2022)
How to gain profits from the operation of the points mall
抽象类、接口
Verilog splicing operation symbol
对比HomeKit、米家,智汀家庭云版有哪些场景化的体验