[프로그래머스/Java] Lv.0 짝수 홀수 개수

febCho·2024년 3월 28일
0

코딩테스트

목록 보기
147/253
post-thumbnail

문제

정수가 담긴 리스트 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++){
            if(num_list[i]%2 == 0){
                answer[0]++;
            }else{
                answer[1]++;
            }
        }
        
        return answer;
    }
}

결과

profile
Done is better than perfect.

0개의 댓글