If you are targeting iOS 5+ (what covers the whole iOS world), best use NSOrderedSet
. It removes duplicates and retains the order of your NSArray
.
Just do
NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:yourArray];
You can now convert it back to a unique NSArray
NSArray *uniqueArray = orderedSet.array;
Or just use the orderedSet because it has the same methods like an NSArray like objectAtIndex:
, firstObject
and so on.
A membership check with contains
is even faster on the NSOrderedSet
than it would be on an NSArray
For more checkout the NSOrderedSet Reference