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

  • SEARCH
  • Home
  • 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 6691099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:42:45+00:00 2026-05-26T05:42:45+00:00

I want to add objects from NSArray *small whenever they don´t exist in NSArray

  • 0

I want to add objects from NSArray *small whenever they don´t exist in NSArray *big.
But the output shows that the objects from *small which exist in *big are added. I have tried x isEqualToString: x == NO (I know it is not same as range.location and hasPrefix, but it is weird that even hiya -> is added when it does exist in *big)
and range.location = NSNotFound and x hasPrefix: x == NO. But neither of them does work. Why?

By the way, *small contains fewer objects than *big. Does it matter?

The codes below:

NSArray *big = [[NSArray alloc] initWithObjects:@"hello ->hi", @"hiya ->", @"hiya ->whatever", @"hiya -> howdy", @"good day ->hello", @"nope, but ->no", @"however ->what", @"May ->april", @"mai ->", nil];
NSArray *small = [[NSArray alloc] initWithObjects: @"match", @"hiya ->",@"hiya ->", @"hiya ->",@"nope, but ->", @"however ->", @"May ->", nil];


NSString *same;

NSMutableArray *newWords = [[NSMutableArray alloc]init];
newWords = [NSMutableArray  arrayWithArray: big];

NSLog (@"big: %@", big);


int i;
for (i = 0; i<[small count]; i++)

{
    same = [small objectAtIndex:i];
    for (NSString *s in big)
    {
        //NSRange ran = [s rangeOfString:same];
        //if (ran.location =NSNotFound)
        //if ([s isEqualToString: same] == NO)

        if ([s hasPrefix:same] == NO)

        {
            [newWords addObject:same];
            break;
        }
    }
}

The output shows:

2011-10-17 19:21:56.855 scanner2[4018:207] newWords: (
    "hello ->hi",
    "hiya ->",
    "hiya ->whatever",
    "hiya -> howdy",
    "good day ->hello",
    "nope, but ->no",
    "however ->what",
    "May ->april",
    "mai ->",
    match,
    "hiya ->",
    "hiya ->",
    "hiya ->",
    "nope, but ->",
    "however ->",
    "May ->"
)

edit: I even tried if ([x compare: x ] !=NSOrderedSame), but only hiya -> is added thrice to *newWords.

  • 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-26T05:42:46+00:00Added an answer on May 26, 2026 at 5:42 am

    You are comparing each string in Small to each string in Big. If ANY of the strings in Big are not the same as the one you’re comparing, then you’re adding it to Big. But actually, you want to add it if ALL of the strings are not the same. So, you need to wait until after checking the whole big array.

    Try this instead:

    for (NSString *same in small) {
        BOOL found = NO;
        for (NSString *s in big) {
            if ([s hasPrefix:same] == YES) {
                found = YES;
                break;
            }
        }
        if (!found) {
           [newWords addObject:same];
        }
    }
    

    To be pedantic, I should add that if the arrays are going to be MUCH bigger than the demo strings you’re showing, then you might want to maintain Big as a sorted array, so that you can find presence or absence much faster.

    For example:

    NSArray *big = [[NSArray alloc] initWithObjects:@"hello ->hi", @"hiya ->", @"hiya ->whatever", @"hiya -> howdy", @"good day ->hello", @"nope, but ->no", @"however ->what", @"May ->april", @"mai ->", nil];
    NSArray *small = [[NSArray alloc] initWithObjects: @"match", @"hiya ->",@"hiya ->", @"hiya ->",@"nope, but ->", @"however ->", @"May ->", @"match",nil];
    
    NSDate *start = [NSDate date];
    
    NSMutableArray *newWords = [NSMutableArray  arrayWithArray: big];
    
    for (NSString *same in small) {
        BOOL found = NO;
        for (NSString *s in big) {
            if ([s isEqualToString:same] == YES) {
                found = YES;
                break;
            }
        }
        if (!found) {
            [newWords addObject:same];
        }
    }
    
    NSLog(@"Time for original method: %fms ",-[start timeIntervalSinceNow]*1000);
    
    start = [NSDate date];
    /*
     static NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch;
     NSLocale *currentLocale = [NSLocale currentLocale];
     NSComparator sort = ^(id string1, id string2) {
     NSRange string1Range = NSMakeRange(0, [string1 length]);
     return [string1 compare:string2 options:comparisonOptions range:string1Range locale:currentLocale];
     };*/
    
    NSComparator sort = ^(id s1, id s2) { return [s1 compare:s2]; };
    
    newWords = [NSMutableArray arrayWithArray: 
                [big sortedArrayUsingComparator:sort]]; 
    
    for (NSString *same in small) {
        NSUInteger i = [newWords indexOfObject:same 
                                 inSortedRange:NSMakeRange(0, [newWords count]) 
                                       options:NSBinarySearchingInsertionIndex 
                               usingComparator:sort ];
        if (![same isEqualToString:[newWords objectAtIndex:i]] ) {
            [newWords insertObject:same atIndex:i];
        }
    }
    //    }
    NSLog(@"Time for new method: %fms ",-[start timeIntervalSinceNow]*1000);
    [big release];
    [small release];
    

    Obviously if you’re doing this multiple times, just keep big as a sorted mutable array, rather than re-sorting it each time.

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

Sidebar

Related Questions

I have a situation where i want to add LinePragmas to CodeDom objects. But
In my WPF application I want to add multiple 3D objects from xaml files.
I have a list of string values that I want add to a hashtable
I have numbers in javascript from 01(int) to 09(int) and I want add 1(int)
I have viewport3D and I want to add multiple 3d objects defined in xaml
I have a NSArray containing objects. I want to create a secondary NSArray containing
I'm trying to fetch objects from core data that are not in a given
I want to add some rows to a database using Linq to SQL, but
I have a dictionary with three objects, and I want to add this dictionary
I want to add some functionality track certain calls to ActiveX object methods in

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.