OnlyCodeMatters OnlyCodeMatters - 3 months ago 6
Swift Question

finding elements from two CGPoint arrays that contain the same x value

I know how to find an array that contains the same elements of two arrays, i.e,

let resultArray = firstArray.filter(secondArray.contains)


But if both
firstArray
and
secondArray
are
CGPoint
arrays, how do I find elements that contain the same x values in both?

Answer
let secoundArrayXPoints = secondArray.map{$0.x}
let resultArray = firstArray.filter{ secoundArrayXPoints.contains($0.x) }
Comments