Re: [question] Checking for duplicate entries in NSMutableArray
Re: [question] Checking for duplicate entries in NSMutableArray
- Subject: Re: [question] Checking for duplicate entries in NSMutableArray
- From: Chris Suter <email@hidden>
- Date: Mon, 8 Oct 2007 07:54:18 +1000
On 07/10/2007, at 10:51 AM, email@hidden wrote:
Hi *,
what would be an efficient and smart way to check for duplicate
entries in an NSMutableArray? The objects of this array are of a
class, that has only a bunch of strings as variables (i.e.
firstName, lastName, memberOfGroup...).
I've read about using NSDictionary when no duplicates are wanted at
all, however, my case lies differently: duplicates must be allowed
but should be affirmed by the user, namely in the case, that there
actually is someone of the same name in the same
group...yet...still another person.
So i need a way to quickly and efficiently check the entire Array
for an object which values are identical to the ones that just were
entered. My thoughts were drifting into some sort of *creating a
hash over the strings and store that in an additional NSString
variable*...but i'm not sure if that's really efficient (checking
the entire Array for String comparisons upon the creation (-->
insertion) of a new object) and also i wouldn't be sure how to go
about that).
As always, i appreciate any feedback and have nothing but <3 for
this list!
Gute Nacht aus Berlin,
Malte Philipp A.
One option is that you use a dictionary with arrays as values. For
example (typed in Mail):
id v = [myDict objectForKey:key];
if (v) {
if (shouldAddIfDuplicate) {
if ([v isKindOfClass;[NSMutableArray class]])
[v addObject:newObject];
else
[myDict setObject:[NSMutableArray arrayWithObjects:v,
newObject, nil] forKey:key];
} else
[myDict setObject:newObject forKey:key];
In the example above I only store them as arrays when there are more
than one but you could always store arrays for the values.
- Chris
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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