1

I have the following 2 arrays. Array A with i.e. 10 id's, and the other array, Array B with 300 id's with all the corresponding data.

I want to retrieve all the data from B with id's which are stated in the array A.

I could just loop all entries in array B on every entry in array A, but this looks a little bit heavy for such simple task. What would be the best solutions in this case?

Thnx!

4 Answers 4

2

If you don't need an ordered array you could switch to an hash table (eg. NSSet) which has a much faster lookup time (strong bias towards O(1)). Otherwise you had to loop through the whole array and check all of its members.

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

Comments

1

Probably use a predicate (a, b are your arrays):

NSPredicate* filter = [NSPredicate predicateWithFormat: @"self IN %@", a];
NSArray* result = [b filteredArrayUsingPredicate: filter];

Although, it might only work if the objects in your arrays are simple things like strings.

1 Comment

Superb, this is what is was looking for
1

When you are filling array B you can instead make it a dictionary. Assuming ids are unique you can then get value of the object for key in array A from array B. This will avoid looping and is sort of equivalent to hash table.

The returned value will then have the corresponding data for the particular unique id.

Comments

0

I don't see how you are going to do it otherwise if you are using arrays. The only way to access the right element is by looping through the array.

4 Comments

Please stop making tiny title edits which result in grammatically incorrect titles. If you're going to make these massive bulk edits, take the extra 2 seconds and capitalize the title after you strip pseudotags, and make sure the resulting title actually makes sense.
Sure I will do that. Thank you. I don't see how the grammar is getting more incorrect though
Make sure the first word of the title is capitalized. If possible, turn the title into a question. Instead of leaving How to do <x>, make it How can I <x>? (note the question mark). Also scan the body of the question for obvious mistakes, remove signatures/tag-lines if they exist, capitalized "i" where appropriate, etc etc. Many of your edits are good, but you should focus on making the question as good as you can, not leaving the bulk of editing for somebody else to do.
Sure, I will put more effort into it. It just bugs me with these redundant titles everywhere.

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.