I have random data (names) in strings that I need to compare. Data (names) will not always be the same but it will be in this format.
For example, I have two NSString like this:
NSString *string1= @"Jordan Mike Liam Taylor Jill Gordon Phil Mark";
NSString *string2= @"Marcus Tony Taylor Anny Keenan Brittany Gordon Mike";
Based on these 2 strings, we can see that both strings contain Mike| Taylor | Gordon
So the count of same data between these two strings is 3. However I am not able to get it work via code.
The following below is what I have thus far. I feel that I am close but not quite there and would really appreciated some help from the community. Thank you in advance!
NSMutableArray *tempArray= [[NSMutableArray alloc] init];
[tempArray addObject:string1];
[tempArray addObject:string2];
NSCountedSet *bag = [[NSCountedSet alloc] initWithArray:tempArray];
NSString *mostOccurring;
NSUInteger highest = 0;
for (NSString *s in bag)
{
if ([bag countForObject:s] > highest)
{
highest = [bag countForObject:s];
mostOccurring = s;
}
}
NSLog(@"Most frequent string: %d", highest);
EDIT CODE
NSUInteger highest = 1;
NSUInteger theCount=0;
for (NSString *s in bag)
{
if ([bag countForObject:s] > highest)
{
highest = [bag countForObject:s];
mostOccurring = s;
}
if (highest ==2)
{
theCount++;
}
}
NSLog(@"Most frequent string: %d", theCount);
I am late to answer this question, but have a look here :