프로그래머스 1단계 - 폰켓몬

Genie·2021년 11월 18일
0

프로그래머스

목록 보기
2/2

🔍 문제 설명

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

💡 Trouble Shooting

int 배열을 어떻게 하면 중복을 제거해서 정렬할 수 있을까? 를 고민했다.
문제에서 최대 종류라고 했기 때문에 같은 종류는 안뽑고, 다른 종류만 뽑는 경우의 수를 생각해내는게(중복을 제거해서) 이 문제의 핵심이라고 생각한다.

set 의 길이를 알고싶으면, set.size() 하면 된다..

📝 소스코드

import java.util.*;

class Solution {
    public int solution(int[] nums) {
        int answer = 0;
         Set<Integer> set = new HashSet<Integer>();
            for(int i : nums)
                set.add(i);
        
        answer = Math.min(nums.length/2 , set.size());
        return answer;
    }
}
profile
차근차근

0개의 댓글