• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Deleting duplicates in NSMutableArray
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Deleting duplicates in NSMutableArray


  • Subject: Re: Deleting duplicates in NSMutableArray
  • From: King Chung Huang <email@hidden>
  • Date: Sun, 04 Jul 2004 16:52:54 -0600

I usually just create a new NSSet from my NSArray, then get an array out of it. Not sure if it's the most efficient way of getting rid of duplicates, but it's certainly the easiest!

NSArray *objectsWithDuplicates = ...;
NSSet *duplicatesRemover = [NSSet setWithArray:objectsWithDuplications];
NSArray *uniqueObjects = [duplicatesRemove allObjects];

King Chung Huang
Learning Commons
University of Calgary

On Jul 4, 2004, at 4:04 PM, Johan Kool wrote:

Hi all,

I am a bit stuck with this piece of code that has to remove duplicate NSStrings from a NSMutableArray. It fails when multiple duplicates come right after each other in the array. It has to do something with shifting indexes when an object is deleted, but can't think of how to solve it.

// Delete duplicates
for (i=0; i < [mutArray count]; i++ ) {
for (j=(i+1); j < [mutArray count]; j++) {
if ([[mutArray objectAtIndex:i] isEqualTo:[mutArray objectAtIndex:j]]) {
[mutArray removeObjectAtIndex:j];
}
}
}

What do I do wrong?!

Thanks already!

Johan
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


  • Follow-Ups:
    • Re: Deleting duplicates in NSMutableArray
      • From: Allan Odgaard <email@hidden>
References: 
 >Deleting duplicates in NSMutableArray (From: Johan Kool <email@hidden>)

  • Prev by Date: Re: Change value of NSNumber?
  • Next by Date: Getting RTFD data into an NSData object.
  • Previous by thread: Deleting duplicates in NSMutableArray
  • Next by thread: Re: Deleting duplicates in NSMutableArray
  • Index(es):
    • Date
    • Thread