프로그래머스 폰켓몬 종류 번호 개수 구하는 문제
import java.util.HashSet;
class Solution {
public int solution(int[] nums) {
int length = nums.length; // 폰켓몬 종류 번호 담긴 배열의 길이
int count = length/2; // 가질 수 있는 폰캣몬 수
// 중복 제거해주는 HashSet 선언
HashSet<Integer> ponketmon = new HashSet<>();
// 중복 제거해서 HashSet에 추가
for(int num:nums){
ponketmon.add(num);
}
// 폰켓몬 종류 번호의 개수 구하기
return Math.min(ponketmon.size(),count);
}
}
같은 번호를 가진 폰켓몬 중복 없애기위해 HashSet 사용