当前位置:网站首页>Find the most repeated element in the string
Find the most repeated element in the string
2022-06-29 09:18:00 【swag_ Special actor】
Ideas ( Limited ability , Compare dishes , Hope for more guidance )
loop , The first cycle is written directly count = 1, Then the second loop looks after the current element ( Be careful : It's dealt with here , Do not iterate over duplicate elements , Such as the first cycle array[0] Element is s, Of the first cycle array[5] The element is still s Then the following operations will not be carried out , Because it has been traversed .), Find it count Add 1, Count the number of repetitions of each element into the object .
then , Traverse the object that stores the number of character repetitions , Find the maximum number of character repetitions , Go through it again, find the element whose repetition times are equal to the maximum value, and take it out .( The reason for not merging here is ,s The number of repetitions is 4,d The number of repetitions is also 4, This will leave one out )
Pure loop method , Complexity soars

Borrow regular expressions , The complexity is not low

Source code
// Find the most repeated character in the string and output the number of repetitions
function getMaxRepeatStr (String) {
let checked = [] // Checked elements
var stringArray = String.split('')
let obj = {
} // An object that holds the number of character repetitions
for (var i = 0; i < stringArray.length; i++) {
if (!checked.includes(stringArray[i])) {
// The second cycle is carried out only after it has not been checked
let count = 1 // Repeat the number
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]) // Mark checked
} else {
continue
}
}
// Find the maximum number of repetitions
let maxVal = 0
Object.keys(obj).forEach(element => {
if (obj[element] > maxVal) {
maxVal = obj[element]
}
})
// Get the most repeated character
let result = {
}
Object.keys(obj).forEach(element => {
if (obj[element] === maxVal) {
result[element] = obj[element]
}
})
console.log(' The most repeated characters in the string are :', result)
}
function getMaxRepeatStr (String) {
let checked = [] // Checked elements
var stringArray = String.split('')
let obj = {
} // An object that holds the number of character repetitions
for (var i = 0; i < stringArray.length; i++) {
if (!checked.includes(stringArray[i])) {
// It is not handled until it has been inspected
let reg = new RegExp(stringArray[i], "ig");
let count = String.match(reg).length;
checked.push(stringArray[i]) // Mark checked
obj[stringArray[i]] = count
} else {
continue
}
}
// Find the maximum number of repetitions
let maxVal = 0
Object.keys(obj).forEach(element => {
if (obj[element] > maxVal) {
maxVal = obj[element]
}
})
// Get the most repeated character
let result = {
}
Object.keys(obj).forEach(element => {
if (obj[element] === maxVal) {
result[element] = obj[element]
}
})
console.log(' The most repeated characters in the string are :', result)
}
getMaxRepeatStr('sdfjhkjhkjlsdsdfsd')
getMaxRepeatStr('dfghjjjklmnssss')
边栏推荐
- NPM common commands
- H5 soft keyboard problem
- Handwriting Redux thunk
- JS to obtain basic information about the width and height of an image or Base64
- AugFPN:改進多尺度特征學習用於目標檢測
- LFFD:一种用于边缘检测的轻量化快速人脸检测器
- (转)MySQL: ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
- ActiveMQ message component publish subscribe redelivery message redelivery
- 微信小程序自定义多项选择器
- Verilog 表达式
猜你喜欢

H5 soft keyboard problem

SSD改進CFENet

Enrollment brochure for system integration project management engineer certification in July 2022

Verilog 大小端以及 +:使用

H5软键盘问题

Detecting and counting tiny faces

一般乘法器设计,verilog code

keras转tf.keras中VGG19 input_shape

手写 redux-thunk

Product manager certification enrollment brochure (NPDP) in July 2022
随机推荐
(转)MySQL: ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
JS to obtain basic information about the width and height of an image or Base64
Scenario analysis of deadlock during MySQL insert
Detailed version of two-stage target detection principle
查找字符串中重复次数最多的元素
训练查看(问题暂存)
Analysis of c voice endpoint detection (VAD) implementation process
MySQL virtual column
Activemq消息组件发布订阅ReDelivery消息重新投递
pytorch总结学习系列-操作
Verilog expression
Let's make a summary
Handwriting Redux thunk
npm常用命令
July 2022 (advanced soft test) information system project manager certification enrollment Brochure
微信小程序分享页面,分享到朋友圈
MYSQL虚拟列
记微信小程序分享代码片段
JS获取图片或base64的宽高等基本信息
微信小程序wx.navigateBack返回上一页携带参数