프로그래머스 - 중복된 숫자 개수 filter, reduce
·
Algorithm/programmers
filter는 익숙한 문법입니다. '걸러내다' 라는 의미를 갖고 있으며 조건에 맞는 원하는 값을 걸러내는데 사용되며 아래처럼 사용됩니다.func solution(_ array:[Int], _ n:Int) -> Int { return array.filter { $0 == n }.count} reduce로 숫자 더하기let numbers = [1, 2, 3, 4, 5]이 배열의 모든 숫자를 더하고 싶다면 `reduce`를 이렇게 사용할 수 있습니다.let sum = numbers.reduce(0) { (결과, 숫자) in return 결과 + 숫자}- 0은 초기값 입니다.- `{ (결과, 숫자) in 결과 + 숫자 }`는 각 숫자를 어떻게 더할지를 설명하는 규칙 입니다.[1, 2, 3, 4, 5]..
iOS_mobile_developer
'Filter' 태그의 글 목록