백준-Node.js-2751 ❓

송철진·2023년 2월 7일
0

백준-Node.js

목록 보기
7/69

풀이

const fs = require('fs')
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n').map(Number).slice(1)

const solution = arr => {
  return arr.sort((a,b) => a-b).join("\n")
}

console.log(solution(input))

문제도 풀이도 굉~장히 단순하다

근데 왜 아래의 풀이는 안되는 걸까?
객체에 숫자를 key로 추가하면 자동으로 정렬되는 속성을 이용한 건데 틀렸습니다 라고 표시된다.

const fs = require('fs')
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n').slice(1)

const solution = arr => {
    const obj = {}
    for(let el of arr){
        obj[el] = 0
    }
    return Object.keys(obj).join('\n')
}

console.log( solution(input) )
profile
검색하고 기록하며 학습하는 백엔드 개발자

0개의 댓글