function solution(nums) {
// 중복 제거
// 중복 제거한 값의 길이가 더 짧으면 해당 길이. 아니면 n/2마리
const set = [...new Set(nums)]
const count = nums.length/2
return count < set.length ? count : set.length;
}
Set을 중복 제거로만 사용해와서, size라는 메서드가 있는걸 몰랐다. 해당 메서드를 사용하면 배열화 > 배열의 길이 구하는 과정을 거치지 않고 바로 원하는 값을 얻을 수 있다.
function solution(nums) {
const setSize = new Set(nums).size;
const max = nums.length/2
return max < setSize ? max : setSize;
}