I have an array full of strings with each string being a name. Some names may be the same while some may be different. The language I'm working in is objective-C. I want to be able to find out which name is most popular from this array (the array will be dynamic based on information given to the app from the user). I am not sure how to make this happen EFFICIENTLY. If someone could expand on this or provide an example, it would be appreciated.
我有一个满是字符串的数组,每个字符串都是一个名称。有些名字可能是相同的,有些可能是不同的。我使用的语言是objective-C。我希望能够从这个数组中找出最受欢迎的名字(这个数组将根据用户给应用的信息动态显示)。我不知道如何有效地实现这一点。如果有人能在此基础上扩展或提供一个例子,我们将不胜感激。
Thank you
谢谢你!
Example:
例子:
NSArray *nameArray= [[NSArray alloc] initWithObjects @"james", @"megan", @"lauren", @"mike" @james", nil];
//james would be the most popular name
3 个解决方案
#1
11
Use NSCountedSet
and then find the object with highest count using countForObject:
method.
使用NSCountedSet,然后使用countForObject:方法找到具有最高计数的对象。
//create count set from array
NSCountedSet *setOfObjects = [[NSCountedSet alloc] initWithArray:yourArrayhere];
//Declaration of objects
NSString *mostOccurringObject = @"";
NSUInteger highestCount = 0;
//Iterate in set to find highest count for a object
for (NSString *strObject in setOfObjects)
{
NSUInteger tempCount = [setOfObjects countForObject:strObject];
if (tempCount > highest)
{
highestCount = tempCount;
mostOccurringObject = strObject;
}
}
Checking the result:
检查结果:
NSLog(@"Most frequent string: %@ with count: %i", mostOccurringObject,highestCount);
Credit goes to @Evan Mulawski answer
下面是@Evan Mulawski的回复
#2
5
I would use a hash table (NSMutableDictionary
in your case), go through the array of strings, use each string as key, and set its value as the number its occurrences in the array. You can keep track of the maximum using a variable (or an array of names if there is multiple names with the same number of occurrences).
我将使用哈希表(在您的例子中是NSMutableDictionary),遍历字符串数组,使用每个字符串作为键,并将其值设置为数组中出现的数字。您可以使用一个变量(如果多个名称的出现次数相同,则使用一个名称数组)来跟踪最大值。
The running time is then linear ( O(n) where n is the number of names in your array ).
运行时间是线性的(O(n),其中n是数组中名称的数量)。
#3
0
To get the number of occurrences.
获取事件的数量。
NSArray *nameArray= [[NSArray alloc] initWithObjects @"james", @"megan", @"lauren", @"mike" @james", nil];
NSCountedSet *set = [[NSCountedSet alloc] nameArray];
#1
11
Use NSCountedSet
and then find the object with highest count using countForObject:
method.
使用NSCountedSet,然后使用countForObject:方法找到具有最高计数的对象。
//create count set from array
NSCountedSet *setOfObjects = [[NSCountedSet alloc] initWithArray:yourArrayhere];
//Declaration of objects
NSString *mostOccurringObject = @"";
NSUInteger highestCount = 0;
//Iterate in set to find highest count for a object
for (NSString *strObject in setOfObjects)
{
NSUInteger tempCount = [setOfObjects countForObject:strObject];
if (tempCount > highest)
{
highestCount = tempCount;
mostOccurringObject = strObject;
}
}
Checking the result:
检查结果:
NSLog(@"Most frequent string: %@ with count: %i", mostOccurringObject,highestCount);
Credit goes to @Evan Mulawski answer
下面是@Evan Mulawski的回复
#2
5
I would use a hash table (NSMutableDictionary
in your case), go through the array of strings, use each string as key, and set its value as the number its occurrences in the array. You can keep track of the maximum using a variable (or an array of names if there is multiple names with the same number of occurrences).
我将使用哈希表(在您的例子中是NSMutableDictionary),遍历字符串数组,使用每个字符串作为键,并将其值设置为数组中出现的数字。您可以使用一个变量(如果多个名称的出现次数相同,则使用一个名称数组)来跟踪最大值。
The running time is then linear ( O(n) where n is the number of names in your array ).
运行时间是线性的(O(n),其中n是数组中名称的数量)。
#3
0
To get the number of occurrences.
获取事件的数量。
NSArray *nameArray= [[NSArray alloc] initWithObjects @"james", @"megan", @"lauren", @"mike" @james", nil];
NSCountedSet *set = [[NSCountedSet alloc] nameArray];