Removing duplicates from NSMutableArray

Just convert the array to an NSSet and back again. A set can’t have duplicates by design.

EDIT:

Note that a set doesn’t have a sorting order. Therefore, you can go cheaper and forgo the order, or go for a slightly more expensive operation but keep the order.

NSArray *hasDuplicates = /* (...) */;
NSArray *noDuplicates = [[NSSet setWithArray: hasDuplicates] allObjects];

Leave a Comment