Asad Khan Asad Khan - 1 year ago 114
Objective-C Question

removing duplicates from array in objective c

I have an array with custom objects. Each array item has a field named "name". Now I want to remove duplicate entries based on this name value.

How should I go about achieving this.

Thanks in advance.

Answer Source

You might have to actually write this filtering method yourself:

@interface NSArray (CustomFiltering)

@implementation NSArray (CustomFiltering) 

- (NSArray *) filterObjectsByKey:(NSString *) key {
   NSMutableSet *tempValues = [[NSMutableSet alloc] init];
   NSMutableArray *ret = [NSMutableArray array];
   for(id obj in self) {
       if(! [tempValues containsObject:[obj valueForKey:key]]) {
            [tempValues addObject:[obj valueForKey:key]];
            [ret addObject:obj];
   [tempValues release];
   return ret;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download