เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่รับสตริงและส่งกลับจำนวนอักขระที่ซ้ำซ้อนในสตริง
ตัวอย่างเช่น − หากสตริงคือ −
const str = 'abcde'
จากนั้นผลลัพธ์ควรเป็น 0
หากสตริงคือ −
const str = 'aaacbfsc';
จากนั้นผลลัพธ์ควรเป็น 3
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const str = 'aaacbfsc';
const countRedundant = str => {
let count = 0;
for(let i = 0; i < str.length; i++){
if(i === str.lastIndexOf(str[i])){
continue;
};
count++;
};
return count;
};
console.log(countRedundant(str)); ผลลัพธ์
ต่อไปนี้เป็นผลลัพธ์ในคอนโซล -
3