开发者

Check duplicate objects in NSMutableArray?

开发者 https://www.devze.com 2022-12-24 07:03 出处:网络
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.)

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 ad开发者_高级运维d
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


Remember an NSMutableArray is an NSArray too.

if (![theArray containsObject:theNumber]) {
  // does not contain.
}

(If you want unique objects and don't care about insertion order, NSMutableSet is a more efficient container.)


To answer your second question: no, you don't really need to check whether the set contains the object already. NSMutableSet will do this for you when you call addObject:. It may have a more efficient way of doing this (since it has access to the internal data structure), so you may actually end up with a slight performance benefit by letting NSMutableSet handle it.

If nothing else, it's less code you have to write, and that's always nice.


This depends on how big your array is likely to get. You can check whether something's already in the array by using -containsObject:. This can be as bad as O(n*logn) on the length of the array and is thus no good for super big arrays, but keeps the code simple to maintain.

The generic way to do this though in general for arbitrary size data sets is to keep an NSMutableSet alongside the array. Check the set for the existence of the item before adding to the array. If it's already in the set, don't add it. If not, add it to both.

Of course, if you don't care about order and only uniqueness, then don't use the array at all, just use the Set.


I have a category on NSMutableArray

@interface NSMutableArray (CategoryName)

- (void)addObjectUnique:(id)anObject;

@end

@implementation NSMutableArray (CategoryName)

- (void)addObjectUnique:(id)anObject
{
  if ([self containsObject:anObject]) {
    return;
  }
  [self addObject:anObject];
}

@end


Try this:

// Number to add is newNumber, myArray is your Mutable array
if(![myArray containsObject:newNumber])
{
  [myArray addObject:myNumber];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号