본문 바로가기
CS (컴퓨터 사이언스)/Algorithm (알고리즘)

[알고리즘] 프로그래머스 해시 - 폰켓몬

by dreamer10457 2024. 6. 3.
반응형

https://school.programmers.co.kr/learn/courses/30/lessons/1845

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

function solution(nums) {
    var answer = 0;
    let map=new Map();
    
    for(let num of nums){
        if(map.has(num)){
          map.set(num,map.get(num)+1)
        }else {
            map.set(num,1)
        }
    }

    answer=Math.min(nums.length/2 , map.size)     
  
    return answer;
}

 

 

function solution(nums) {
  const max = nums.length / 2;
  const arr = [...new Set(nums)];

  return arr.length > max ? max : arr.length
}
반응형