Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vibinnair/e68df45981386a025f8e60ae09ed5bf8 to your computer and use it in GitHub Desktop.
Save vibinnair/e68df45981386a025f8e60ae09ed5bf8 to your computer and use it in GitHub Desktop.
https://www.hackerrank.com/challenges/fp-filter-positions-in-a-list/problem
import Foundation
func filterOddElements(_ list: [Int]) -> [Int] {
if list.count <= 1 {
return []
}
let elements = list.prefix(2)
let oddElement = elements.last!
let newList = Array(list.dropFirst(2))
return [oddElement] + filterOddElements(newList)
}
print(filterOddElements([5, 3, 4, 6, 7, 9, 8]))
print(filterOddElements([2, 5, 3, 4, 6, 7, 9, 8]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment