[프로그래머스/Java] 폰켓몬

괜찮아요?·2023년 3월 29일
0

programmers

목록 보기
6/23

프로그래머스 폰켓몬 링크

Solved process

  1. Check the max number to pick
  2. Deduplication using 'HastSet'
  3. Comparing

Code

import java.util.HashSet;

class Solution {
    public int solution(int[] nums) {
        int pick = (nums.length)/2; //max number
        System.out.println(pick);

        //deduplication
        HashSet<Integer> set = new HashSet<>();

        for(int num: nums){
            set.add(num);
        }

        //return ans
        if (set.size() >= pick){
            return pick;
        } else {
            return set.size();
        }
    }
}
profile
할 수 있어요

0개의 댓글