3

I have an NSDictinary look like this:

NSArray *duplicates = @[@{@"name": @"a", @"id": @"123"}, @{@"name": @"c", @"id": @"234"}, @{@"name": @"a", @"id": @"431"}, @{@"name": @"c", @"id": @"983"}, @{@"name": @"a", @"038"}];

How to remove the ditionaries which have the same name. For example, I want to remove the duplicate "a" and "c". I want the result to be like this:

NSArray *duplicates = @[@{@"name": @"a", @"id": @"123"}, @{@"name": @"c", @"id": @"234"}];
yong ho
  • 3,892
  • 9
  • 40
  • 81
  • Did you search for duplicate questions? Why did you add the duplicates in the first place? – Wain Jun 24 '16 at 06:35
  • refers this link please http://stackoverflow.com/questions/1025674/the-best-way-to-remove-duplicate-values-from-nsmutablearray-in-objective-c – Ajharudeen khan Jun 24 '16 at 06:40

2 Answers2

8

Have you try this code

NSArray *duplicates = @[@{@"name": @"a"}, @{@"name": @"c"}, @{@"name": @"a"}, @{@"name": @"c"}, @{@"name": @"a"}];
NSSet *set = [NSSet setWithArray:duplicates];
NSArray *uniqueArray = [set allObjects];
Nirav D
  • 71,513
  • 12
  • 161
  • 183
balkaran singh
  • 2,754
  • 1
  • 17
  • 32
1

Just use following code for remove duplicates values.

your_array = [self groupsWithDuplicatesRemoved:(NSArray *)your_array myKeyParameter:@"your_key_name"];

You have to just call groupsWithDuplicatesRemoved this method with key name.

- (NSMutableArray *) groupsWithDuplicatesRemoved:(NSArray *)  groups myKeyParameter:(NSString *)myKeyParameter {
    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 = [NSString stringWithFormat:@"%@", [group objectForKey:myKeyParameter]]; //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's encountered
        }
    }
    return groupsFiltered;
}

Hope, this is what you're looking for. Any concern get back to me. :)

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
  • If you have multiple keys into a dictionary and that all data into a single array. Now you want to get unique array according to only one key. Then you must have to use this trick. – Meet Doshi Jun 24 '16 at 06:49
  • For example, you have an array like this.. `NSArray *duplicates = @[@{@"name": @"a", @"name1": @"c"}, @{@"name": @"b", @"name1": @"d"}, @{@"name": @"c", @"name1": @"d"}, @{@"name": @"d"}, @{@"name1": @"d"}];`... Now you want to get unique array based on "name1" key.. at that time, you must have to use this code like this way... `unique_array = [self groupsWithDuplicatesRemoved:(NSArray *)duplicates myKeyParameter:@"name1"];` – Meet Doshi Jun 24 '16 at 06:51