I'm trying to loop through my array and find duplicate objects based on the name properties of the objects in the array. Once a duplicate is found I need to combine these objects, the name and unit values stay the same, I just want to add the quantity values together. At this point I want to remove the two duplicates and add the new object to the array instead.
If its two difficult to remove two objects and add another (might mess with the index?) then the new objects could be added to a filtered array, as long as when a duplicate isnt found that is also added to this array. So the new array will contain all of my previous values, with duplicate values combine on quantity.
I have this so far:
NSMutableSet* existingNames = [NSMutableSet set];
NSMutableArray* filteredArray = [NSMutableArray array];
for (id object in ingredientsArray)
{
if (![existingNames containsObject:[object name]])
{
[existingNames addObject:[object name]];
NSLog(@"%@", @"DUPLICATE FOUND");
//create new object to store the new info in.
ingredient *info = [[ingredient alloc] initWithname:[object name] quantity:[/* How do I add the quanitity values of the two objects that are duplicates here? */] unit:[object unit]];
//add this to filtered array.
[filteredArray addObject:object];
//remove object from array.
[ingredientsArray removeObject:object];
}
}
thanks