Sava Mazăre Sava Mazăre - 1 year ago 108
Objective-C Question

Remove duplicates from array comparing the properties of its objects

Suppose I have a class Event, and it has 2 properties: action (NSString) and date (NSDate).

And suppose I have an array of Event objects. The problem is that "date" properties can match.

I need to remove the duplicates, meaning that 2 different objects with the same date IS a duplicate.

I can remove duplicates in any array of strings or nsdates, they are easy to compare. But how to do it with complex objects, where their properties are to be compared?

Don't ask me what I did so far, cos' the only thing coming in my mind is a bubble sort, but it's a newbie solution, and slow.

Quite any help is highly appreciated (links, tuts, code).

Thanks in advance.


Thanks to dasblinkenlight, I have made a custom method:

- (NSArray *)removeDuplicatesInArray:(NSArray*)arrayToFilter{

NSMutableSet *seenDates = [NSMutableSet set];
NSPredicate *dupDatesPred = [NSPredicate predicateWithBlock: ^BOOL(id obj, NSDictionary *bind) {
YourClass *e = (YourClass*)obj;
BOOL seen = [seenDates containsObject:e.propertyName];
if (!seen) {
[seenDates addObject:e.when];
return !seen;
return [arrayToFilter filteredArrayUsingPredicate:dupDatesPred];

is the name of your class the object belongs to, and
is the property of that object you are going to compare.

Suppose self.arrayWithObjects contains the objects of YourClass.

After populating it, use

self.arrayWithObjects = [self removeDuplicatesInArray:self.arrayWithObjects];

and you are done.

All credits to dasblinkenlight.

Answer Source

You can create an NSMutableSet of dates, iterate your event list, and add only events the date for which you have not encountered before.

NSMutableSet *seenDates = [NSMutableSet set];
NSPredicate *dupDatesPred = [NSPredicate predicateWithBlock: ^BOOL(id obj, NSDictionary *bind) {
    Event *e = (Event*)obj;
    BOOL seen = [seenDates];
    if (!seen) {
    return !seen;
NSArray *events = ... // This is your array which needs to be filtered
NSArray *filtered = [events filteredArrayUsingPredicate:dupDatesPred];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download