查找字符串中出现的次数

时间:2022-07-27 04:17:55

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:

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

根据这两个字符串,我们可以看到两个字符串都包含Mike |泰勒|戈登

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!

因此,这两个字符串之间的相同数据的计数是3.但是我无法通过代码使其工作。以下是我到目前为止所拥有的内容。我觉得我很亲密,但并不完全在那里,非常感谢社区的一些帮助。先感谢您!

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);

2 个解决方案

#1


2  

I am late to answer this question, but have a look here :

我迟到了回答这个问题,但请看一下:

NSString *string1= @"Jordan Mike Liam Taylor Jill Gordon Phil Mark a";
NSString *string2= @"Marcus Tony Taylor Anny Keenan Brittany Gordon Mike";
NSMutableSet *set1=[NSMutableSet setWithArray:[string1 componentsSeparatedByString:@" "]];
NSMutableSet *set2=[NSMutableSet setWithArray:[string2 componentsSeparatedByString:@" "]];
[set1 intersectSet:set2];

NSArray *intersect=[set1 allObjects];//intersect contains all the common elements

NSLog(@"Common count is  %ld.",[intersect count]);

#2


1  

You don't have to change your code example very much at all to achieve what you want:

您根本无需更改代码示例即可实现您的目标:

    NSString *string1= @"Jordan Mike Liam Taylor Jill Gordon Phil Mark";
    NSString *string2= @"Marcus Tony Taylor Anny Keenan Brittany Gordon Mike";

    NSMutableArray *tempArray= [[NSMutableArray alloc] init];
    [tempArray addObjectsFromArray:[string1 componentsSeparatedByString:@" "]];
    [tempArray addObjectsFromArray:[string2 componentsSeparatedByString:@" "]];

    NSCountedSet *bag = [[NSCountedSet alloc] initWithArray:tempArray];


    NSUInteger repeats = 0;
    NSMutableArray *matches = [[NSMutableArray alloc] init];
    for (NSString *s in bag)
    {
        if ([bag countForObject:s] > 1)
        {
            repeats++;
            [matches addObject:s];
        }
    }
    NSLog(@"Number of names repeated: %ld", repeats);
    NSLog(@"Matches: %@", matches);

#1


2  

I am late to answer this question, but have a look here :

我迟到了回答这个问题,但请看一下:

NSString *string1= @"Jordan Mike Liam Taylor Jill Gordon Phil Mark a";
NSString *string2= @"Marcus Tony Taylor Anny Keenan Brittany Gordon Mike";
NSMutableSet *set1=[NSMutableSet setWithArray:[string1 componentsSeparatedByString:@" "]];
NSMutableSet *set2=[NSMutableSet setWithArray:[string2 componentsSeparatedByString:@" "]];
[set1 intersectSet:set2];

NSArray *intersect=[set1 allObjects];//intersect contains all the common elements

NSLog(@"Common count is  %ld.",[intersect count]);

#2


1  

You don't have to change your code example very much at all to achieve what you want:

您根本无需更改代码示例即可实现您的目标:

    NSString *string1= @"Jordan Mike Liam Taylor Jill Gordon Phil Mark";
    NSString *string2= @"Marcus Tony Taylor Anny Keenan Brittany Gordon Mike";

    NSMutableArray *tempArray= [[NSMutableArray alloc] init];
    [tempArray addObjectsFromArray:[string1 componentsSeparatedByString:@" "]];
    [tempArray addObjectsFromArray:[string2 componentsSeparatedByString:@" "]];

    NSCountedSet *bag = [[NSCountedSet alloc] initWithArray:tempArray];


    NSUInteger repeats = 0;
    NSMutableArray *matches = [[NSMutableArray alloc] init];
    for (NSString *s in bag)
    {
        if ([bag countForObject:s] > 1)
        {
            repeats++;
            [matches addObject:s];
        }
    }
    NSLog(@"Number of names repeated: %ld", repeats);
    NSLog(@"Matches: %@", matches);