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

jonghwan·2022년 10월 5일
0

프로그래머스

목록 보기
23/71
post-thumbnail

1. 문제 설명

짝수 홀수 개수

2. 문제 분석

짝수를 담은 배열, 홀수를 담은 배열 각각 개수를 빈 배열에 넣어주고 반환한다.

3. 나의 풀이

import Foundation

func solution(_ num_list:[Int]) -> [Int] {
    var num1: [Int] = []
    var num2: [Int] = []
    var num3: [Int] = []
    
    for i in num_list {
        if i % 2 == 0 {
            num1.append(i)
        } else {
            num2.append(i)
        }
    }
    
    num3.append(num1.count)
    num3.append(num2.count)
    return num3
}

4. 다른 사람의 풀이

import Foundation

func solution(_ num_list:[Int]) -> [Int] {
    return [num_list.filter{$0 % 2 == 0}.count, num_list.filter{$0 % 2 == 1}.count]
}

빨리 filter와 친해지고 싶다 🔥

0개의 댓글