cocoa - Finding duplicates in an NSMutableArray -


I have a class (color class) that has 2 NSStrings (idNumber and preferred color). There is an NSMutableArray (array color) that holds more than 50,000 color class objects. What is the fastest way to find all duplicate IDGs from all color class objects and return them to the array? Right now I'm using 1 for a loop, which copies the array into color, then filters the copied array using an NSPredicate. It takes 5 minutes to sort the array. How can this be done more efficiently?

The first question is, is there really anything? If not, use a NSMutableSet or NSMutableDictionary (which depends on your app's understanding)

Duplicate Finish The easiest way to stop them is to stop them from being in the first place before adding anything to your NSMutableArray , you can see if the value already exists or not. For example:

  - (zero) adcolor: (NSString *) color withID: (NSString *) ID {NSArray * duplicates = [myArray filtered application [NSPredicate predicateWithFormat: @ "id ==% @ ", id]]; If ([duplicate count]> gt; 0) {// Optionally report an error / throw an exception return; }}  

Otherwise, you might be best to get a list of IDs using valueForKeyPath: , then sort that array, and Then run this to see it duplicate; Soming will be like this:

  - (NSSet *) CheckForuduplicate ID {NSArray * allIDs = [myArray valueForKeyPath: @ "id"]; NSArray * Sorted ID = [allIDs Sorted UsesUser: Selector: @Selector (compare :)); NSString * previousID = Zero; NSMutableSet * Duplicate ID = [NSMutableSet Set]; For (NSSTing * AID in the charted ID) {if (previous ID is on [IID]); {[Duplicate IDEs adject: AID]; } PreviousID = AID; } Return [[duplicate ID copy] autorelease]; }  

Keep in mind that list sorting is still the best, maybe a o (n log (n)) operation. If you can at least keep your items in your list, then you can avoid the cost of sorting them out. Duplicate prevention is best, best after keeping the list organized is best, and the above given algorithm is probably the worst.


Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -