본문 바로가기

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

[JAVA] 프로그래머스 - 짝수 홀수 개수

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

 

제한사항

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

 

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;
    }
}

 

 

 

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

https://school.programmers.co.kr/learn/courses/30/lessons/120824