나의 풀이
import java.util.*;
class Solution {
public int solution(int[] nums) {
int choice_cnt = nums.length/2;
Integer[] arr = Arrays.stream(nums).boxed().toArray(Integer[]::new);
Set<Integer> set = new HashSet<Integer>(Arrays.asList(arr));
int kind_cnt=set.size();
if(choice_cnt>kind_cnt){
return kind_cnt;
} else{
return choice_cnt;
}
}
}
다른 사람의 풀이
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)));
}
}
▶ boxed() ->IntStream 기본형 스트림을 Stream<Integer> 타입으로 바뀌준다.
▶ collectingAndThen() 연구 필요
'프로그래머스(자바) > LV.1(자바)' 카테고리의 다른 글
크레인 인형 뽑기 → 열접근<for each문 2개>★★+break문★ (0) | 2022.12.21 |
---|---|
신규 아이디 추천→ 정규식★★ for java (0) | 2022.12.21 |
키패드 누르기★★ (0) | 2022.12.20 |
숫자 짝궁→char타입 - int타입, 내림차순 정렬시 0이 맨 앞에 오는 경우→ 모든 요소를 '0'으로 되어 있다 . (0) | 2022.12.20 |
과일장수→ 내림차순 정렬★★ + 우측에서 좌측으로 접근★★ (0) | 2022.12.20 |