In Swift, I want to loop over an array and compare each element to the previous and/or next. For each comparison I will either produce a new element or nothing. Is there \"funct
Using flatMap
let a = [ 1,2,2,3,5,4,2,5,7,9,5,3,8,10 ] let r = a.enumerated().flatMap { (_ offset: Int, _ element: Int) -> Int? in guard offset > 0 else { return nil } if element < a[offset-1] && element < a[offset+1] { return offset } return nil }