[프로그래머스 LV0] 짝수는 싫어요

jonghwan·2022년 10월 7일
0

프로그래머스

목록 보기
21/71
post-thumbnail

1. 문제 설명

짝수는 싫어요

2. 문제 분석

1부터 n까지 홀수를 배열에 담아 반환해준다.

3. 나의 풀이

import Foundation

func solution(_ n:Int) -> [Int] {
    var arr: [Int] = []
    
    for i in 1...n {
        if i % 2 == 1 {
            arr.append(i)
        }
    }
    
    return arr
}

4. 다른 사람의 풀이

import Foundation

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

filter ! filter ! filter !

0개의 댓글