kakasoo

[node.js] 두 개 뽑아서 더하기 ( 프로그래머스 레벨2 ) 본문

프로그래밍/알고리즘 풀이

[node.js] 두 개 뽑아서 더하기 ( 프로그래머스 레벨2 )

카카수(kakasoo) 2021. 6. 26. 12:44
반응형
// 프로그래머스 level2 두 개 뽑아서 더하기
function solution(numbers) {
    let answer = [];
    numbers.map((el1, i) => {
        numbers.map((el2, j) => {
            if ((i !== j) && (!answer.includes(el1+el2))) answer.push(el1 + el2);
        })
    })
    return answer.sort((o1,o2) => o1 - o2);
}

for문 두 개로 하여 인덱스가 다른 두 값을 더한 것을 계속 더해 간다.
중복을 제거하면서 더한 후 정렬해서 내보내기만 하면 된다.
레벨 2가 맞던가 싶다.
너무 오래 전에 풀어서 기억이 가물가물하다.

반응형