How to remove duplicate records / objects from NSArray


Example code how to remove duplicate records / objects from NSArray and return a NSMutableArray with a unique record set.
Xcode 4.2.1
Mac OS X 10.7.2
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.