Cho mảng kích thước n, yêu cầu tìm phần tử chiếm đa số (majority element). Phần tử chiếm đa số là phần tử xuất hiện nhiều hơn [n/2] lần.
Ví dụ
const arr = [2, 4, 2, 2, 2, 4, 6, 2, 5, 2];
const majorityElement = (arr = []) => {
const threshold = Math.floor(arr.length / 2);
const map = {};
for (let i = 0; i < arr.length; i++) {
const value = arr[i];
map[value] = map[value] + 1 || 1;
if (map[value] > threshold)
return value
};
return false;
};
console.log(majorityElement(arr));Kết quả
Kết quả in ra console:
2