function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const a = A.reduce((acc, curr, index) => {
if (!acc[curr]) acc[curr] = [];
acc[curr].push(index);
return acc;
}, {});
let max = 0;
let maxKey;
Object.entries(a).forEach(([key, arr]) => {
if (arr.length > max) {
max = arr.length;
maxKey = arr[0];
}
});
if (max > A.length/2) {
return maxKey;
}
return -1;
}