๋ฐ์ํ
ํ๋ก๊ทธ๋๋จธ์ค LV.0 ๋ชจ์
์ค๋ณต๋ ์ซ์ ๊ฐ์
๋ฌธ์ ์ค๋ช
์ ์๊ฐ ๋ด๊ธด ๋ฐฐ์ด array์ ์ ์ n์ด ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, array์ n์ด ๋ช ๊ฐ ์๋ ์ง๋ฅผ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด๋ณด์ธ์.
์ ํ ์ฌํญ
- 1 ≤ array์ ๊ธธ์ด ≤ 100
- 0 ≤ array์ ์์ ≤ 1,000
- 0 ≤ n ≤ 1,000
์ ์ถ๋ ฅ ์
array | n | result |
[1, 1, 2, 3, 4, 5] | 1 | 2 |
[0. 2, 3, 4] | 1 | 0 |
์ ์ถ๋ ฅ ์ ์ค๋ช
์ ์ถ๋ ฅ ์ #1
- [1, 1, 2, 3, 4, 5] ์๋ 1์ด 2๊ฐ ์์ต๋๋ค.
์ ์ถ๋ ฅ ์ #2
- [0, 2, 3, 4] ์๋ 1์ด 0๊ฐ ์์ต๋๋ค.
์ ์ถ
import Foundation
func solution(_ array:[Int], _ n:Int) -> Int {
return array.filter{$0 == n}.count
}
filter ํจ์๋ฅผ ์ฌ์ฉํด ๋ฐฐ์ด์ ์์ ์ค์์ n๊ณผ ๊ฐ์ ๊ฒฝ์ฐ๋ฅผ ์ฐพ๊ณ count ํจ์๋ฅผ ์ฌ์ฉํด ๊ทธ ๊ฐ์๋ฅผ ๊ตฌํ๋ค.
๋ฐ์ํ
'โจ๏ธ Language > swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] ํ๋ก๊ทธ๋๋จธ์ค LV.0 ๋ ์์ ํฉ (1) | 2022.11.29 |
---|---|
[Swift] ํ๋ก๊ทธ๋๋จธ์ค LV.0 ๋จธ์ฑ์ด๋ณด๋ค ํค ํฐ ์ฌ๋ (1) | 2022.11.29 |
[Swift] Codility Lesson 5 - CountDiv (0) | 2022.07.26 |
[Swift] Codility Lesson 5 - PassingCars (0) | 2022.07.26 |
[Swift] Codility Lesson 4 - MissingInteger (0) | 2022.07.24 |