Hello,
I have 2 arrays of CGPoints. Array 'A' has a list of all available CGPoints, and array 'B' has a list of CGPoints I want removed from array 'A'. How can I iterate through array A to remove all values that are also in array B?
Hello,
I have 2 arrays of CGPoints. Array 'A' has a list of all available CGPoints, and array 'B' has a list of CGPoints I want removed from array 'A'. How can I iterate through array A to remove all values that are also in array B?
Anyone?
I think you should combine two ideas. First merge the two arrays and then get the unique set of values. One quick way to do this is add the two arrays and then use the fact that an NSSet will only contain unique values (it strips the dupes automatically). I am running out now so I've not had time to check this but here is some code. Please let me know if this solves the issue by clicking the link below or feel free to update the original post for clarification.
//merge the two arrays
let array = a + b //where a and b are array of CGPoints
let uniqueArray = NSSet(array: array).allObjects
I don't think that's what I'm trying to do. Array B will contain 3-4 objects that are also in array A. I want to remove all objects from array A that get matched from array B. Think of array A as a full list of points and array B are the points that need to be removed from array A.
I would write something like this.
var arrA: [CGPoint] = [/* actual content */]
var arrB: [CGPoint] = [/* actual content */]
arrA = arrA.filter {!arrB.contains($0)}If the size of arrB may be far larger than 3-4, using Set may improve performance.
extension CGPoint: Hashable {
public var hashValue: Int {
//Better hashValue may improve performance
return self.x.hashValue &* 257 &+ self.y.hashValue
}
}With making CGPoint hashable:
var setB = Set(arrB)
arrA = arrA.filter {!setB.contains($0)}