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

☑️ 문제

프로그래머스 1845

☑️ 풀이

  • HashSet을 이용하여 중복된 폰켓몬을 다 제거하도록 하였다.
//시간: 5.88ms
//메모리: 91.6MB

import java.util.*;

class Solution {
    public int solution(int[] nums) {
        int size = nums.length / 2;
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i< nums.length; i++) {
            set.add(nums[i]);
        }

        if (set.size() < size) {
            return set.size();
        } else {
            return size;
        }
    }
}

© 2021. All rights reserved.

yaejinkong의 블로그