Algorithm: Storing the number of key / value pair in NSDictionary

New to Cocoa, and perhaps not knowing all the classes available that already have this functionality neatly wrapped in an OO class, here's an algorithm request. What's the best bet for counting the number of times a particular key occurs in an array of multiple instances NSDictionary

?

Essentially my data structure (in this case NSArray

) can contain multiple instances NSDictionary

at any given time, each with the same keys, potentially different values. Some values ​​are repeated. I would like to know how many times a specific key / value appears. Example:

{
  foo => 1,
  bar => 2
}
{
  foo => 1,
  bar => 3
}
{
  foo => 2,
  bar => 1
}

      

In this case, I'm wondering what foo=>1

happened 2 times, and foo=>2

- 1 time. Is instantiating a NSCountedSet

better way to do this? Perhaps a C-linked list?

+1


source to share


3 answers


NSDictionary * dict1 = [[NSDictionary alloc] initWithObjectsAndKeys:
                        [NSNumber numberWithInt:1], @"foo",
                        [NSNumber numberWithInt:2], @"bar", nil];
NSDictionary * dict2 = [[NSDictionary alloc] initWithObjectsAndKeys:
                        [NSNumber numberWithInt:1], @"foo",
                        [NSNumber numberWithInt:3], @"bar", nil];
NSDictionary * dict3 = [[NSDictionary alloc] initWithObjectsAndKeys:
                        [NSNumber numberWithInt:2], @"foo",
                        [NSNumber numberWithInt:1], @"bar", nil];
NSArray * arrayOfDictionaries = [[NSArray alloc] initWithObjects:
                                 dict1, dict2, dict3, nil];

// count all keys in an array of dictionaries (arrayOfDictionaries):

NSMutableDictionary * countKeys = [[NSMutableDictionary alloc] initWithCapacity:0];
NSCountedSet * counts = [[NSCountedSet alloc] initWithCapacity:0];

NSArray * keys;
NSString * pairString;
NSString * countKey;
for (NSDictionary * dictionary in arrayOfDictionaries)
{
    keys = [dictionary allKeys];
    for (NSString * key in keys)
    {
        pairString = [NSString stringWithFormat:@"%@->%@", key, [dictionary valueForKey:key]];
        if ([countKeys valueForKey:pairString] == nil)
        {
            [countKeys setValue:[NSString stringWithString:pairString] forKey:pairString];
        }
        countKey = [countKeys valueForKey:pairString];
        { [counts addObject:countKey]; }
    }
}

NSLog(@"%@", counts);

[counts release];
[countKeys release];

[arrayOfDictionaries release];
[dict1 release];
[dict2 release];
[dict3 release];
      



+2


source


You might want to rethink how you structure your data. I would track something like this by adding an NSArray instead of trying to detect it later. You can create a new class to handle and delete data so that you can store your own data.



+4


source


NSCountedSet *keyCounts = [NSCountedSet set];
for (NSDictionary *dict in myDictionaries)
    [keyCounts unionSet:[NSSet setWithArray:[dict allKeys]]];

      

+1


source







All Articles