0

I am attempting to de-dupe an NSArray of NSDictionaries based on specific keys in the dictionaries. What I have looks something like this:

NSDictionary *person1 = [NSDictionary dictionaryWithObjectsAndKeys:@"John", @"firstName", @"Smith", "lastName", @"7898", @"employeeID"];
NSDictionary *person2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Eric", @"firstName", @"Johnson", "lastName", @"1718" @"employeeID"];
NSDictionary *person3 = [NSDictionary dictionaryWithObjectsAndKeys:@"John", @"firstName", @"Smith", "lastName", @"1153", @"employeeID"];

NSMutableArray *personArray = [NSArray arrayWithObjects:person1, person2, person3, nil];

// insert some code to de-dupe personArray based SOLELY on the firstName and lastName keys

Notice how there are two employees with the same name but different ID's. What I would like to do is just get back a new array with only person1 and person2, since person3 has the same data - I just don't care about the "employeeID" value in this particular problem.

Any ideas? Thanks!

-Matt

mag725
  • 695
  • 2
  • 9
  • 22

2 Answers2

5

Add a Person class which inherits from NSDictionary and implement isEqual: ignoring the ID key, cast your dictionaries to this class, then create an NSSet from your Person objects.

w-m
  • 10,772
  • 1
  • 42
  • 49
0

In this very case I would create a temporary NSMutableDictionary and add each personX dictionary as object and its "firstName"'s value as key. This way, a personX dictionary will be replaced if another personX with the same name is added to the temporary. Then get the array from the temporary dictionary with -allValues.

sigsegv
  • 447
  • 8
  • 17
  • I appreciate your fast response! I think this would work well in this case, the only problem, which I forgot to specify is that I am dealing with many thousands of said person objects. Your method works, though I am not certain how scalable it is. – mag725 Oct 21 '10 at 17:06
  • I may be wrong, but it is understood by me that NSDictionary behave most of the time as hash map, with amortized insertion time of O(1). Initialize the dictionary with with a capacity proportional with the expected number of unique entry to prevent eccessive rehashing and test it. Memory shouldn't be a problem either, because what you insert is actually a reference. If you have lots of entries and you keep retrieving and manipulating them, maybe Core Data will suit your needs better. – sigsegv Oct 21 '10 at 17:32