The best way to remove duplicate values (NSString
) from NSMutableArray
in Objective-C?
Is this the easiest and right way to do it?
uniquearray = [[NSSet setWithArray:yourarray] allObjects];
iosnsmutablearrayobjective c
The best way to remove duplicate values (NSString
) from NSMutableArray
in Objective-C?
Is this the easiest and right way to do it?
uniquearray = [[NSSet setWithArray:yourarray] allObjects];
Best Answer
Your
NSSet
approach is the best if you're not worried about the order of the objects, but then again, if you're not worried about the order, then why aren't you storing them in anNSSet
to begin with?I wrote the answer below in 2009; in 2011, Apple added
NSOrderedSet
to iOS 5 and Mac OS X 10.7. What had been an algorithm is now two lines of code:If you are worried about the order and you're running on iOS 4 or earlier, loop over a copy of the array: