프로그래머스 - 코딩테스트

[Java][프로그래머스] - LVL.0 중복된 숫자 개수

Denny Code 2023. 1. 26. 21:44


정답 1

이제는 딱 구상이 떠오른다. IntStream 에 filter.

import java.util.stream.IntStream;

class Solution {
    public int solution(int[] array, int n) {
        return IntStream.of(array).filter(d -> d == n).toArray().length;
    }
}

정답 2

IntStream 이 아닌 Arrays 에 filter 를 사용할 수 도 있다.

import java.util.Arrays;

class Solution {
    public int solution(int[] array, int n) {
        return (int) Arrays.stream(array).filter(i -> i == n).count();
    }
}