I'm looking for a way to prevent an array from having duplicate references to the same object in it. I don't mean duplicate values - that would be fine.
For example, I know in Apple's SpriteKit framework, an SKNode object stores its children in an array (var children: [SKNode]
), and adding a node to a parent twice causes the program to crash.
let parent = SKNode()
let child1 = SKNode()
let child2 = SKNode()
parent.addChild(child1)
parent.addChild(child2) // Allowed, although child2 == child1.
parent.addChild(child1) // Causes a crash.
This is the exact kind of behavior I am wanting to emulate. How would I manage this? Is it possible without having O(n) complexity from having to compare each reference?