2

I have an array (NSArray or NSMutableArray doesn't matter): SpecID of specific files IDs (109234, etc.). And I have a large array of all files IDs : FilesID.
I need to check whether FilesID contains all the elements of SpecID.

So the question what is the fastest and most efficient way of doing this except simple comparing all the elements to each other in a loop. May be there are some standard method or efficient algorithm?

1 Answer 1

12

You could use sets:

NSSet *specIDs = [NSSet setWithArray:specIDarray];
NSSet *fileIDs = [NSSet setWithArray:fileIDarray];

if ([specIDs isSubsetOfSet:fileIDs])
{
    // Your file IDs contains every ID found in specIDarray
}

For this to work efficiently, the objects should ideally be NSNumber objects, or if they are custom objects, they should override both hash and isEqual:. The efficiency of sets depends mostly on having a good hash. The Foundation classes, e.g. NSNumber, NSString etc have good hashes.

Also, if you can, load your IDs directly into sets rather than converting them from arrays as this will be slightly more efficient, but otherwise the above is probably as simple as it would get. There may be specialised algorithms which would perform better but only explore those options if the above is too slow.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.