Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

หาจำนวนครั้งขององค์ประกoyantที่ปรากฏมากที่สุดใน 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