เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่ใช้อาร์เรย์ของตัวอักษรและส่งคืนจำนวนองค์ประกอบที่ปรากฏเป็นจำนวนครั้งมากที่สุดในอาร์เรย์
ตัวอย่าง
รหัสสำหรับสิ่งนี้จะเป็น −
let arr = [2, 8, 4, 8, 6, 4, 7, 8];
const countOccurence = arr => {
const max = arr.reduce((acc, val) => {
return Math.max(acc, val);
}, -Infinity);
const count = arr.filter(el => {
return el === max;
});
const { length } = count;
return length;
};
console.log(countOccurence(arr)); ผลลัพธ์
เอาต์พุตในคอนโซล −
3