270. Dominator

아현·2021년 8월 16일
0

Algorithm

목록 보기
282/400



1. JavaScript



// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');


function solution(A) {
    const counter = {};
    const standard = A.length / 2;
    for (var i = 0; i < A.length; i++) {
        if (counter[A[i]]) {
            counter[A[i]].push(i);
        } else {
            counter[A[i]] = [i]; //인덱스 저장
        }
        if (counter[A[i]].length > standard) {
            return counter[A[i]][0];
        }
    }
    return -1;
}




2. Python




# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    dic = {}
    half = len(A)/2
    for i in range(len(A)):
        if A[i] in dic:
            dic[A[i]] += 1
        else:
            dic[A[i]] = 1
        if dic[A[i]] > half:
                return i
    return -1
    
    
profile
For the sake of someone who studies computer science

0개의 댓글