코딩테스트/2024 코딩 테스트
[ 프로그래머스 ] 배열의 원소 삭제하기
주연배
2024. 3. 1. 21:43
문제
풀이
noneMatch : 모든 요소가 주어진 조건을 만족하지 않는지 검사할 때 사용된다.
anyMatch : 최소 한개의 요소가 주어진 조건에 맞는지 검사할 때 사용된다.
allMatch : 모든 요소들이 주어진 조건을 모두 만족하는가를 검사할 때 사용된다.
과정
import java.util.stream.IntStream; class Solution { public int[] solution(int[] arr, int[] delete_list) { return IntStream.of(arr) .filter(i -> IntStream.of(delete_list).noneMatch(j-> j==i)) .toArray(); } }
noneMatch를 사용해서 delete_list의 원소랑 arr의 원소가 같지 않는 배열만 뽑아내도록 하였다.
.filter(i -> IntStream(delete_list).noneMatch(j -> j==i))
anyMatch를 이용해서도 풀 수 있다!
.filter(i -> !IntStream(delete_list).anyMatch(j -> j==i))