Deepak Bhati Deepak Bhati - 3 months ago 24
iOS Question

remove Duplicate Object from NSMutableArray with keep sorting and all other fields

its My First Question for Apologies in advance for any mistake.

My Question is Remove duplicate Value with keep Same Order that Array have.
Like my array Format is:-

author = "";
content = "After almost";
"document_name" = "20150108";
"news_id" = 280;
author = "";
content = "Content";
"document_name" = "20150108"
"news_id" = 282;

Actually i m getting this array from Core Data and i know about SetPropertiesToFetch like:-

[request setResultType:NSDictionaryResultType];
[request setReturnsDistinctResults:YES];
[request setPropertiesToFetch:@[@"news_id"]];

but from this i only get news_id field but i need all other field too.
also i know about NSSet

NSArray *arrayWithNoDuplicates = [[NSSet setWithArray:papersObject.paperSubject] allObjects];

due to this i lost my sort order that i did in predicate.

and while searching i found enumerateObjectsUsingBlock

NSMutableArray *filteredManagers = [NSMutableArray array];
[managers enumerateObjectsUsingBlock:^(Manager manager, NSUInteger idx, BOOL stop) {
if (![filteredManagers containsObject:manager]) {
[filteredManagers addObject:manager];

but in same article say that there is no much difference between for loop and block. so i cant use that because i have thousands on data and its not good to put loop to Check and remove data from array.

so anybody know about better solution that remove duplicate object and keep sort with all other fields in dictionary, please please let me know.


For your approach, use NSOrderedSet instead of NSSet:

NSArray *arrayWithNoDuplicates = [NSOrderedSet orderedSetWithArray:papersObject.paperSubject].array;

If you want to get all field, simply remove

[request setPropertiesToFetch:@[@"news_id"]];

Your problem seems to me, that you actually have duplicate entries in your Cora Data store though.