Algorithm for unique objects from an array with sub-objects?

NSArray *cars = ...; NSMutableDictionary *carsByUniqueEnginePowers = NSMutableDictionary dictionary; for (Car *car in cars) { NSNumber *enginePower = NSNumber numberWithInteger:car.engine. Power; carsByUniqueEnginePowers setObject:car forKey:enginePower; } NSArray *carsWithUniqueEnginePowers = carsByUniqueEnginePowers allValues Will pick the last item per engine power To get the first one replace the line carsByUniqueEnginePowers setObject:car forKey:enginePower with this: if (!carsByUniqueEnginePowers objectForKey:power) { carsByUniqueEnginePowers setObject:car forKey:enginePower; }.

NSArray *cars = ...; NSMutableDictionary *carsByUniqueEnginePowers = NSMutableDictionary dictionary; for (Car *car in cars) { NSNumber *enginePower = NSNumber numberWithInteger:car.engine. Power; carsByUniqueEnginePowers setObject:car forKey:enginePower; } NSArray *carsWithUniqueEnginePowers = carsByUniqueEnginePowers allValues; Will pick the last item per engine power. To get the first one replace the line carsByUniqueEnginePowers setObject:car forKey:enginePower; with this: if (!carsByUniqueEnginePowers objectForKey:power) { carsByUniqueEnginePowers setObject:car forKey:enginePower; }.

Try it in a moment...order doesn't really matter as long as it's unique – Javy Nov 15 at 0:12 @Javy: Updated answer to cover "first occurences". – Regexident Nov 15 at 0:16 This seems to sort through an engine array, but I have an array of car objects that have engine objects with a power property...so this would only sort an array of engine objects, correct? – Javy Nov 15 at 0:21 Oh, sorry, misread your question then.

Updated answer. Should fit your needs now. – Regexident Nov 15 at 0:25 Yeah, perfect...I'm definitely going to look into those methods some more.

Thanks a bunch. – Javy Nov 15 at 0:32.

Enumerate the array, building up a list of indices whose objects have previously-unseen power values. Create a set to hold the already-seen powers; -NSArray indexesOfObjectsPassingTest: handles building the index list. Then ask the original array for its objectsAtIndexes:.

NSMutableSet * powerSet = NSMutableSet set; NSIndexSet * indexes; indexes = carArr indexesOfObjectsPassingTest:^BOOL(id car, NSUInteger idx, BOOL *stop) { // valueForKey: will return an NSNumber instance for scalar values. NSNumber * powerNum = car engine valueForKey:@"power"; // This power is already in the set; pass over this car. If( powerSet containsObject:powerNum ){ return NO; } // Add the new power to the set and include this car in the index set.

Else { powerSet addObject:powerNum; return YES; } }; NSArray * uniqueEnginePowerCars = carArr objectsAtIndexes:indexes.

Thanks for the help but Regexident's code was what I used. Thanks for the help. – Javy Nov 15 at 0:33 @Javy: Sure thing!

– Josh Caswell Nov 15 at 0:33.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions