[코딜리티] dominator

박재윤·2021년 1월 6일
0

코테준비

목록 보기
18/25

나의 풀이

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;
}

결과

0개의 댓글

관련 채용 정보