1. 내 풀이
set에 값을 집어넣은 후 풀이한 모습입니다.
import java.util.*;
class Solution {
public int solution(int[] nums) {
int answer = 0;
Set<Integer> set = new HashSet<>();
for(int num : nums) {
set.add(num);
}
return Math.min(set.size(), nums.length/2);
}
}
2. 다른 분 풀이
stream을 활용하여 풀이한 모습입니다.
import java.util.Arrays;
import java.util.stream.Collectors;
class Solution {
public int solution(int[] nums) {
return Arrays.stream(nums)
.boxed()
.collect(Collectors.collectingAndThen(Collectors.toSet(),
phonekemons -> Integer.min(phonekemons.size(), nums.length / 2)));
}
}
'코딩테스트 > Programmers' 카테고리의 다른 글
[프로그래머스] 소수 찾기 (0) | 2025.03.05 |
---|---|
[프로그래머스] 기사단원의 무기 (1) | 2025.03.02 |
[프로그래머스] 비밀지도 (0) | 2025.02.28 |
[프로그래머스] k번째수 (1) | 2025.02.28 |
[프로그래머스] 푸드 파이트 대회 (0) | 2025.02.28 |