Removing duplicate strings from array

I have a Cocoa application that contains an NSMutableArray of NSString objects. I want to remove all duplicate entries from the array (i.e., those strings with the same text, but not necessarily the same object addresses).

I'm thinking perhaps that sortedArrayUsingDescriptors: looks promising, but I'm quite confused as to how to use descriptors. The discussion in the documentation is formidable to say the least.

Any suggestions? Thanks in advance.

Eric

MacBook Pro, Mac OS X (10.5.2)

Posted on May 3, 2008 7:36 AM

Reply
4 replies

May 3, 2008 8:26 AM in response to Eric S

Do you want to remove just the duplicates or both the duplicates and the originals as well? If you want to remove just the duplicates then take a peek at NSSet and NSMutableSet.

Now, let me just say that, I don't know Objective-C but in Java, which I do know, NSSet and NSMutableSet use the equals(Object) method to compare objects so that even if two strings are different objects they'll still be considered the same string if equals(Object) returns true. I don't know if this happens in Objective-C but it's worth taking a look.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Removing duplicate strings from array

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.