Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4066210
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:06:21+00:00 2026-05-20T16:06:21+00:00

I have an NSArray of objects , which has a particular property called name

  • 0

I have an NSArray of objects, which has a particular property called name (type NSString).
I have a second NSArray of NSStrings which are names.

I’d like to get an NSArray of all the objects whose .name property matches one of the names in the second NSArray.

How do I go about this, fast and efficiently as this will be required quite often.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:06:21+00:00Added an answer on May 20, 2026 at 4:06 pm

    With your current data structures, you can only do it in O(n^2) time by looping over the first array once for each member of the second array:

    NSMutableArray * array = [NSMutableArray array];
    for (NSString * name in names) {
        for (MyObject * object in objects) {
            if ([[myObject name] isEqualToString:name]) {
                [array addObject:object];
            }
        }
    }
    

    (Alternate as suggested by Stefan: loop over the objects array and ask the names array if it containsObject: for the name of each object.)

    But if this really needs to be faster (really depends on the size of the arrays as much as how often you do it), you can improve this by introducing an NSDictionary that maps the names in the first array to their objects. Then each of those lookups is O(1) and the overall time is O(n). (You’d have to keep this dictionary always in sync with the array of objects, which isn’t hard with reasonable accessors. This technique also has the constraint that the same name can’t appear on more than one object.)

    An alternate way of getting this result (and which doesn’t have that last constraint) is to use an NSSet for your second collection, then walk through the objects array calling containsObject: with each one on the set of names. Whether this technique is better depends on whether your two collections are roughly the same size, or if one is much larger than the other.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have an NSArray which contains custom objects, the objects have an NSString property,
I have an NSArray of objects called MMPlace, which has NSArray of MMProduct objects.
I have an NSArray of NSDictionary objects which I would like to be able
I have a simple NSArray which has some arrays as objects which has some
I have an NSArray of objects, which have a property id . I then
So I have an NSArray with custom objects called Church. And in each church
I have some NSDictionary objects stored in an NSArray called telephoneArray . I fetch
I have an NSArray storing NSDictionaries. Each dictionary has two keys; a name and
I have an array which has 200 objects in it on page load i
I have an NSArray of Object that has an interesting property that I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.