프로그래머스 java 폰켓몬

jieun·2022년 8월 15일
0

java 코테 공부

목록 보기
10/17

해결방법

  1. 폰켓몬의 종류를 알기위해 nums의 중복값 없애기
  2. 최소한으로 가질 수 있는 N/2과 폰켓몬의 종류 중 큰 값 리턴

활용코드

  1. HashSet
HashSet<Integer> hashSet = new HashSet<>();
	for (int i : arr) {
		hashSet.add(i);
	}

전체코드

import java.util.HashSet;
class Solution {
    public int solution(int[] nums) {
        HashSet<Integer> hashSet = new HashSet<>();
		for (int i : nums) {
            hashSet.add(i);
        }
        return hashSet.size()<nums.length/2 ? hashSet.size() : nums.length/2;
    }
}
profile
개발새발 블로그

0개의 댓글