How to remove duplicate records / objects from NSArray

First create and execute a fetch request in the example i will use a Address entity a fetchedObjects array will be returned with duplicate streetnames.


NSEntityDescription *entity = [NSEntityDescription 

                                       entityForName:@"Address" inManagedObjectContext:context];

             

NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

After executing the executeFetchRequest create a NSSet object which by design can only hold unique objects and set the valueForKey to the field with duplicates.


NSSet *uniqueSet = [NSSet setWithArray:[fetchedObjects valueForKey:@"streetName"]];

   
NSMutableArray *uniqueArray = [NSMutableArray arrayWithArray:[uniqueSet allObjects]];                

 

Create a NSMutableArray and use arrayWithArray and allobjects to create the array with unique streetNames.
Taxonomy