Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vibinnair/e75a803f7d9dd1dd29e3a46ea27d9d51 to your computer and use it in GitHub Desktop.
Save vibinnair/e75a803f7d9dd1dd29e3a46ea27d9d51 to your computer and use it in GitHub Desktop.
https://www.hackerrank.com/challenges/fp-filter-array/problem
import Foundation
func filterList(_ upperLimit: Int, _ list: [Int]) -> [Int] {
if list.count <= 0 {
return []
}
let element = list.first!
let newList = Array(list.dropFirst(1))
if element < upperLimit {
return [element] + filterList(upperLimit, newList)
}
return filterList(upperLimit, newList)
}
print(filterList(3, [1, 2, 3, 4]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment