알고리즘/프로그래머스

Lv.1 - 두 개 뽑아서 더하기

hoj0806 2024. 7. 4. 13:37

문제

풀이코드

const solution = (numbers) => {
    let newArr = []
    numbers.map((item, index) => {
        for(let i = 0; i< numbers.length; i++) {
            if(index !== i) {
                newArr.push(item + numbers[i])
            }
        }
    })
    let removeDuplicateArr = [...new Set(newArr)]
    return removeDuplicateArr.sort((a, b) => a - b)
}

풀이

map과 for 반복문을 이용해 배열에서 두개의 수를 뽑아 더할수 있는 모두의 경우를 배열에 저장한뒤 set로 변환시켜 중복을 제거하고 다시 배열로 만들어준뒤에 오름차순으로 정렬한다