Re: Unique strings array
Re: Unique strings array
- Subject: Re: Unique strings array
- From: Roland King <email@hidden>
- Date: Sat, 12 Mar 2011 10:42:07 +0800
On 12-Mar-2011, at 10:33 AM, Stevo Brock wrote:
> Unless I'm missing something, if you have aWord1 = @"the" and aWord2 = @"the", they will both get added to the array/set.
>
You're missing something. Sets use isEqual and aWord1 and aWord2 are 'equal' to each other so only one gets added and the second one gets ignored.
> You can also use an NSMutableDictionary - just
>
> for (NSString* word in wordsArray)
> [dict setObject:anything forKey:word];
>
> NSArray* uniqueWords = [dict allKeys];
>
NSDictionary uses exactly the same method of figuring out whether something already exists in as NSSet does. So all your example does is do the same thing but use more memory doing it because the NSDictionary has to keep the values you don't need.
NSSet is what you want for this.
Roland
> -Stevo
>
>
> On Mar 11, 2011, at 6:19 PM, Dave DeLong wrote:
>
>> Use an NSMutableSet instead. That should have constant lookup time instead of linear (like an array usually is).
>>
>> Dave
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden