IT/Baekjoon Oline Judge
[BOJ] 3052번 - 나머지
개발자희망생고롸파덕
2023. 8. 16. 20:05
사용언어 : javascript - node.js
#제출 코드
const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
const input = fs
.readFileSync(filePath)
.toString()
.trim()
.split('\n')
.map((item) => +item);
solution(input);
function solution(list) {
let remainList = [];
for (let i = 0; i < list.length; i++) {
let remainNum = list[i] % 42;
if (remainList.length === 0) {
remainList.push(remainNum);
} else if (!remainList.includes(remainNum)) {
remainList.push(remainNum);
}
}
console.log(remainList.length);
}
더 짧은 코드로 작성할 수 없나.. 하고 검색하던 중에 엄청난 코드를 발견했다.
## 발견한 코드
const input = require('fs').readFileSync('/dev/stdin').toString().trim().split('\n');
const count = new Set(input.map(x => x % 42)).size;
console.log(count);
출처 : https://gurtn.tistory.com/37
아 맞다 set이...있었지..
저 긴 코드를 한줄로 줄여버렸다.
잊지말자 set...