1 minute read

짝수 홀수 개수

문제 설명

정수가 담긴 리스트 num_list가 주어질 때, num_list의 원소 중 짝수와 홀수의 개수를 담은 배열을 return 하도록 solution 함수를 완성해보세요.


제한사항

  • 1 ≤ num_list의 길이 ≤ 100
  • 0 ≤ num_list의 원소 ≤ 1,000

입출력 예

num_list result
[1, 2, 3, 4, 5] [2, 3]
[1, 3, 5, 7] [0, 4]

입출력 예 설명

입출력 예 #1

  • [1, 2, 3, 4, 5]에는 짝수가 2, 4로 두 개, 홀수가 1, 3, 5로 세 개 있습니다.

입출력 예 #2

  • [1, 3, 5, 7]에는 짝수가 없고 홀수가 네 개 있습니다.

나의 풀이 코드

class Solution {
    public int[] solution(int[] num_list) {
        int even = 0;
        int odd = 0;
        for (int i=0; i<num_list.length; i++){

            if(num_list[i]%2 == 0){
                even++;
            }
            if(num_list[i]%2 == 1){
                odd++;
            }
        }
        int[] answer = {even,odd};
        return answer;
    }
}

배운점

class Solution {
    public int[] solution(int[] num_list) {
        int[] answer = new int[2];

        for(int i = 0; i < num_list.length; i++)
            answer[num_list[i] % 2]++;

        return answer;
    }
}

남이 푼 코드를 보면 홀짝인지 구분을 한뒤 그 인덱스의 answer배열 요소를 1씩 증가하는 식으로 한것 같다.

import java.util.stream.IntStream;
import java.util.Arrays;

class Solution {
    public int[] solution(int[] numList) {
        return IntStream.of((int) Arrays.stream(numList).filter(i -> i % 2 == 0).count(), (int) Arrays.stream(numList).filter(i -> i % 2 == 1).count()).toArray();
    }
}

또다른 남이 푼 코드를 보면 num_list를 스트림으로 변환하고 짝수와 홀수를 필터링하여 각각의 개수를 계산한 다음 배열로 변환한다.

Leave a comment