Asad Khan Asad Khan - 8 months ago 37
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.


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;