I am adding objects (NSNumbers in this case) to an NSMutableArray and I wanted to check what the best way is to check for duplicates in the array before adding. (i.e.)
Number to add
if (NSMutableArray does not contain Number) {
add Number
}
EDIT:
Many thanks, I had a good luck at NSArray this morning but totally missed "containsObject". That would have done just fine, but having looked at NSMutableSet thats far more what I was looking for. One final question if I may:
while([mySet count] < 5) {
NSNumber *numberToAdd = [NSNumber numberWithInt:random() %10];
[mySet addObject:numberToAdd];
}
I don't think it actually matters, but is it better to check if the set "containsObject" or just throw away the duplicate and carry on.
while([mySet count] < 5) {
NSNumber *numberToAdd = [NSNumber numberWithInt:random() %10];
if(!mySet containsObject:numberToAdd) [mySet addObject:numberToAdd];
}
Again much appreciated, thats really cool and will save me a heap of time.
gary