Removing duplicate key values ​​from a dictionary array

I am making a Facebook API request to return all album names from a specific Facebook group. I am returning an array of dictionaries with 3 keys / values, one of which is the key "name" that maps to the album name, as well as the keys "id" and "created_time".

The only problem is that for some reason I am returning duplicates of the "nominal" values ​​of the albums ... but only a couple. And when I go to the Facebook page, there is only one copy of this album, not a single duplicate.

In addition, their “id” values ​​are different, but this is only the first dictionary from the duplicate group that has a Facebook identifier that actually indicates valid data, other Facebook identifier values ​​simply do not return anything when you execute the Facebook graph with them, so this is the first of the duplicates i want.

How can I remove these useless duplicate dictionaries from my array and save the one that has a valid Facebook id? Thank you :)

+4
source share
1 answer

, , , "" faceBook, . , , , .

, :

-(NSMutableArray *) groupsWithDuplicatesRemoved:(NSArray *)  groups {
    NSMutableArray * groupsFiltered = [[NSMutableArray alloc] init];    //This will be the array of groups you need
    NSMutableArray * groupNamesEncountered = [[NSMutableArray alloc] init]; //This is an array of group names seen so far

    NSString * name;        //Preallocation of group name
    for (NSDictionary * group in groups) {  //Iterate through all groups
        name =[group objectForKey:@"name"]; //Get the group name
        if ([groupNamesEncountered indexOfObject: name]==NSNotFound) {  //Check if this group name hasn't been encountered before
            [groupNamesEncountered addObject:name]; //Now you've encountered it, so add it to the list of encountered names
            [groupsFiltered addObject:group];   //And add the group to the list, as this is the first time it encountered
        }
    }
    return groupsFiltered;
}
+6

All Articles