Almudhafar
Almudhafar

Reputation: 887

Merge two NSDictionary without Repetition

I need to merge two dictionaries without repetition

The first one like:

{
  "Posts": [
    {
      "id": "1",
      "Title": "First post",
      "Category": "News"
    },
    {
      "id": "2",
      "Title": "Second post",
      "Category": "News"
    },
    {
      "id": "3",
      "Title": "Third post",
      "Category": "News"
    }
  ]
}

And the second like:

{
  "Posts": [
    {
      "id": "3",
      "Title": "Third post",
      "Category": "News"
    },
        {
      "id": "4",
      "Title": "Fourth post",
      "Category": "News"
    },
        {
      "id": "5",
      "Title": "Fifth post",
      "Category": "News"
    }
  ]
}

How I can merge without repeating the third entry?

Can anyone help

Upvotes: 1

Views: 391

Answers (2)

serdaryillar
serdaryillar

Reputation: 413

  NSDictionary *dic1 = @{
          @"Posts" : @[
                  @{
                          @"id" : @"1", @"Title" : @"First post", @"Category" : @"News"
                  }, @{
                          @"id" : @"2", @"Title" : @"Second post", @"Category" : @"News"
                  }, @{
                          @"id" : @"3", @"Title" : @"Third post", @"Category" : @"News"
                  }
          ]
  };

  NSDictionary *dic2 = @{
          @"Posts" : @[
                  @{
                          @"id" : @"3", @"Title" : @"Third post", @"Category" : @"News"
                  }, @{
                          @"id" : @"4", @"Title" : @"Fourth post", @"Category" : @"News"
                  }, @{
                          @"id" : @"5", @"Title" : @"Fifth post", @"Category" : @"News"
                  }
          ]
  };

  NSMutableArray *keys = [NSMutableArray new];

  [dic1 enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

      [keys addObject:dic1[ key ]];
  }];

  [dic2 enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

      [keys addObject:dic2[ key ]];
  }];

  NSMutableArray *totalResult = [NSMutableArray new];

  [keys enumerateObjectsUsingBlock:^(NSArray *obj, NSUInteger idx, BOOL *stop) {

      [obj enumerateObjectsUsingBlock:^(id obj2, NSUInteger idx2, BOOL *stop2) {
          NSArray *filtered = [totalResult filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(id == %@)", obj2[ @"id" ]]];
          if ( !filtered.count ) {
              [totalResult addObject:obj2];
          }
      }];

  }];

  NSLog(@"");

enter image description here

Upvotes: 2

Blind Ninja
Blind Ninja

Reputation: 1081

Merge both dictionaries' keys (two separate array retrieved by dict.allKeys ) , and create a new dictionary. But if you are getting similar keys in allKeys , you have to create your own system to pick one out two (or more if you have more than two dictionaries) respected values.

NSDictionary *dict1=[NSDictionary dictionary];
NSDictionary *dict2=[NSDictionary dictionary];

NSArray *arr1=[dict1 allKeys];
NSArray *arr2=[dict2 allKeys];

NSArray *allKeysArray=[arr1 arrayByAddingObjectsFromArray:arr2];

NSMutableDictionary *newDict=[[NSMutableDictionary alloc]init];

for (NSString *aKey in allKeysArray)
{
    BOOL bothArrayContainSameKey=NO;
    if ([arr1 containsObject:aKey] && [arr2 containsObject:aKey])
    {
        bothArrayContainSameKey=YES;
        //needs special treatment
    }
    else if ([arr1 containsObject:aKey])
    {
        [newDict setObject:dict1[aKey] forKey:aKey];
    }
    else if ([arr2 containsObject:aKey])
    {
        [newDict setObject:dict2[aKey] forKey:aKey];
    }
}

Upvotes: 1

Related Questions