[Algorithm] 45 week(12.05 ~ 12.11) 1/3

Dev_min·2022년 12월 5일
0

algorithm

목록 보기
144/157

모의고사

function solution(answers) {
    let answer = [];

    const personA = [1,2,3,4,5];
    const personB = [2,1,2,3,2,4,2,5];
    const personC = [3,3,1,1,2,2,4,4,5,5];
    
    const aCount = getMaxCount(answers, answers.length, personA, personA.length);
    const bCount = getMaxCount(answers, answers.length, personB, personB.length);
    const cCount = getMaxCount(answers, answers.length, personC, personC.length);
    
    const counts = [aCount, bCount, cCount];
    
    const maxCount = Math.max(...counts);
    
    counts.forEach((count, index) => {
        if(count === maxCount){
            answer.push(index + 1);
        }
    });


    return answer
}

const getMaxCount = (list, listLength, personAnswers, personAnswersLength) => {    
    let loopCount = listLength;
    let sameValue = 0;
    for(let i = 0; i < loopCount; i++){
        const idx = i % personAnswersLength;
        if(list[i] === personAnswers[idx]){
            sameValue++
        }
    }
    
    return sameValue
}
profile
TIL record

0개의 댓글