카테고리 없음
[ JS 코딩연습] 포켓몬
L.Joey
2024. 5. 13. 10:32
문제(출처: 프로그래머스)
N 개의 포켓몬 중에서 N/2 개의 유니크한 포켓몬만 선택가능하다.
▶ 내가 푼 방식
function solution(nums) {
const selectNum = Math.floor(nums.length/2)
const unique = [... new Set(nums)].length
return unique < selectNum ? unique : selectNum;
}
▶ 다른 유저가 푼 방식
// 유저 1
function solution(nums) {
const noDuplicatePokemon = new Set(nums);
const pokemonVarietyCount = noDuplicatePokemon.size;
const pokemonCounts = nums.length;
return pokemonVarietyCount > pokemonCounts/2 ? pokemonCounts/2 : pokemonVarietyCount;
}
// 유저 2
// 유저 3
▶ 배운 것들
- new Set() 을 한후 .size 를 이용해서 크기 확인가능