백준1764 - 듣보잡

ieunjung·2020년 8월 3일
post-thumbnail

https://www.acmicpc.net/problem/1764

문제 파악

듣도 못한 사람 명단과 보도 못한 사람 명단 중에서 듣도 보도 못한 사람 명단을 구하는 것이 문제의 목표이다.
말이 헷갈리는데 간단하게 말하자면, 두 문자열 배열에서 중복 문자열을 골라내면 된다.

실패 코드

function solution(n, m, peopleList) {
    const notListenedList = peopleList.slice(0, n);
    const notSeenList = peopleList.slice(n);

    const answer = notListenedList.filter(el => {
        return notSeenList.includes(el);
    });

    answer.sort();

    console.log(answer.length);
    for (const el of answer) {
        console.log(el);
    }
}

두 문자열을 Array 배열로 두고 includes을 이용해서 중복 문자열을 체크했는데 런타임 에러가 났다.

정답 코드

function solution(n, m, peopleList) {
    const notListenedList = new Set(peopleList.slice(0, n));
    const notSeenList = new Set(peopleList.slice(n));

    let answer = [];
    for (const el of notListenedList) {
        if(notSeenList.has(el)){
            answer.push(el)
        }
    }

    answer.sort();

    console.log(answer.length);
    for (const el of answer) {
        console.log(el);
    }
}

Set을 이용해서 has()로 요소 포함 유무를 체크하니 에러가 나지 않고 해결되었다.

profile
Done is better than perfect

0개의 댓글