ปัญหา
เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่รับสตริง ฟังก์ชันของเราควรส่งคืนอาร์เรย์ขององค์ประกอบสองอย่าง โดยองค์ประกอบแรกจะเป็นอักขระที่ทำให้จำนวนการปรากฏต่อเนื่องกันมากที่สุดในสตริง และอันดับที่สองคือจำนวนที่ปรากฏ
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const str = 'tdfdffddffsdsfffffsdsdsddddd';
const findConsecutiveCount = (str = '') => {
let res='';
let count=1;
let arr = []
for (let i=0;i<str.length;i++){
if (str[i]===str[i+1]){
count++
} else {
if (arr.every(v=>v<count)){
res=str[i]+count
}
arr.push(count)
count=1
}
}
return !res?['',0]:[res.slice(0,1),res.slice(1)*1];
};
console.log(findConsecutiveCount(str)); ผลลัพธ์
['f', 5]