ปัญหา
เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่ใช้อาร์เรย์ของสตริง ฟังก์ชันของเราควรลบอักขระที่ซ้ำกันซึ่งปรากฏต่อเนื่องกันในสตริงและส่งคืนอาร์เรย์สตริงที่แก้ไขใหม่
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const arr = ["kelless", "keenness"];
const removeConsecutiveDuplicates = (arr = []) => {
const map = [];
const res = [];
arr.map(el => {
el.split('').reduce((acc, value, index, arr) => {
if (arr[index] !== arr[index+1]) {
map.push(arr[index]);
}
if (index === arr.length-1) {
res.push(map.join(''));
map.length = 0
}
}, 0);
});
return res;
}
console.log(removeConsecutiveDuplicates(arr)); ผลลัพธ์
[ 'keles', 'kenes' ]