Reputation: 894
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.
Upvotes: 0
Views: 338
Reputation: 5953
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.
Upvotes: 4
Reputation: 1306
Your logic is bad. You should end the full loop to add the element, you are adding it too soon. You need to check that everyone in big has not this prefix (not some of them).
Upvotes: 1