Objective c 目标C:计算对象在数组中出现的次数?
我需要执行我认为是基本的功能,但我找不到任何关于如何执行的文档。请帮忙 我需要计算某个对象在数组中出现的次数。见示例:Objective c 目标C:计算对象在数组中出现的次数?,objective-c,arrays,count,Objective C,Arrays,Count,我需要执行我认为是基本的功能,但我找不到任何关于如何执行的文档。请帮忙 我需要计算某个对象在数组中出现的次数。见示例: array = NSArray arrayWithObjects:@"Apple", @"Banana", @"Cantaloupe", @"Apple", @"DragonFruit", @"Eggplant", @"Apple", @"Apple", @"Guava",nil]retain]; 如何遍历数组并计算它找到字符串@“Apple”的次数 感谢您的帮助 一个简单而
array = NSArray arrayWithObjects:@"Apple", @"Banana", @"Cantaloupe", @"Apple", @"DragonFruit", @"Eggplant", @"Apple", @"Apple", @"Guava",nil]retain];
如何遍历数组并计算它找到字符串@“Apple”的次数
感谢您的帮助 一个简单而具体的答案:
int occurrences = 0;
for(NSString *string in array){
occurrences += ([string isEqualToString:@"Apple"]?1:0); //certain object is @"Apple"
}
NSLog(@"number of occurences %d", occurrences);
附言:马丁·巴巴乔夫的回答也很好。使用块的迭代速度更快,但在这种特殊情况下,元素太少,我想没有明显的增益。尽管如此,我还是会使用它:)再使用一个解决方案,使用块(工作示例):
我鼓励您将它们放入字典(Objective C的地图版本)。字典的键是对象,值应该是计数。它当然应该是一本易变的字典。如果找不到该项,请添加该项并将计数设置为1。我对Rob的答案投了赞成票,但我想添加一些代码,希望能对您有所帮助
NSArray *array = [[NSArray alloc] initWithObjects:@"A", @"B", @"B", @"B", @"C", @"D", @"E", @"M", @"X", @"X", nil];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
for(int i=0; i < [array count]; i++) {
NSString *s = [array objectAtIndex:i];
if (![dictionary objectForKey:s]) {
[dictionary setObject:[NSNumber numberWithInt:1] forKey:s];
} else {
[dictionary setObject:[NSNumber numberWithInt:[dictionary objectForKey:s] intValue]+1 forKey:s];
}
}
for(NSString *k in [dictionary keyEnumerator]) {
NSNumber *number = [dictionary objectForKey:k];
NSLog(@"Value of %@:%d", k, [number intValue]);
}
NSArray*array=[[NSArray alloc]initWithObjects:@“A”,“B”,“B”,“C”,“D”,“E”,“M”,“X”,“X”,nil];
NSMutableDictionary*dictionary=[[NSMutableDictionary alloc]init];
对于(int i=0;i<[数组计数];i++){
NSString*s=[array objectAtIndex:i];
if(![dictionary objectForKey:s]){
[dictionary setObject:[NSNumber numberWithInt:1]forKey:s];
}否则{
[dictionary setObject:[NSNumber numberWithInt:[dictionary objectForKey:s]intValue]+1 forKey:s];
}
}
for(在[dictionary keyEnumerator]中为NSString*k){
NSNumber*number=[dictionary objectForKey:k];
NSLog(@“值%@:%d”,k,[number intValue]);
}
如果数组按问题陈述中的顺序排序,则无需使用字典
当您看到两个连续元素相同时,只需执行1次线性扫描并增加计数器,就可以更有效地找到唯一元素的数量
字典解是O(nlog(n)),而线性解是O(n)
下面是线性解决方案的一些伪代码:
array = A,B,B,B,B,C,C,D,E,M,X,X #original array
array = array + -1 # array with a dummy sentinel value to avoid testing corner cases.
# Start with the first element. You want to add some error checking here if array is empty.
last = array[0]
count = 1 # you have seen 1 element 'last' so far in the array.
for e in array[1..]: # go through all the elements starting from the 2nd one onwards
if e != last: # if you see a new element then reset the count
print "There are " + count + " " + last elements
count = 1 # unique element count
else:
count += 1
last = e
使用
NSCountedSet
;它将比字典更快,并且被设计来解决这个问题
NSCountedSet *cs = [NSCountedSet new];
for(id anObj in someArray)
[cs addObject: anObj];
// then, you can access counts like this:
.... count = [cs countForObject: anObj]; ...
[cs release];
正如@bbum所说,使用一个NSCounted集合。有一个初始值设定项,它将数组直接转换为计数集:
NSArray *array = [[NSArray alloc] initWithObjects:@"A", @"B", @"X", @"B", @"C", @"D", @"B", @"E", @"M", @"X", nil];
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:array];
NSLog(@"%@", countedSet);
NSLog输出:
(D[1],M[1],E[1],A[1],B[3],X[2],C[1])
仅访问项目:
count = [countedSet countForObject: anObj]; ...
如果您希望它更通用,或者您希望在数组中计算相等/不同的对象,请尝试以下操作: 符号“!”计数不同的值。如果希望值相同,请删除“!” 希望这对社区有帮助
我用它在uitableview中添加了正确数量的节 关于@bbum和@Zaph的完整代码
NSArray *myArray = [[NSArray alloc] initWithObjects:@"A", @"B", @"X", @"B", @"C", @"D", @"B", @"E", @"M", @"X", nil];
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:myArray];
for (NSString *item in countedSet) {
int count = [countedSet countForObject: item];
NSLog(@"the String ' %@ ' appears %d times in the array",item,count);
}
谢谢。你可以这样做
NSArray *array = [[NSArray alloc] initWithObjects:@"A", @"B", @"X", @"B", @"C", @"D", @"B", @"E", @"M", @"X", nil];
NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:array];
NSArray *uniqueStates = [[orderedSet set] allObjects];
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:array];
for(int i=0;i<[uniqueStates count];i++){
NSLog(@"%@ %d",[uniqueStates objectAtIndex:i], [countedSet countForObject: [uniqueStates objectAtIndex:i]]);
}
NSArray*array=[[NSArray alloc]initWithObjects:@“A”,“B”,“X”,“B”,“C”,“D”,“B”,“E”,“M”,“X”,nil];
NSOrderedSet*orderedSet=[NSOrderedSet orderedSetWithArray:array];
NSArray*唯一状态=[[orderedSet]AllObject];
NSCountedSet*countedSet=[[NSCountedSet alloc]initWithArray:array];
对于(int i=0;i我刚刚遇到了一个非常老的问题。我建议使用NSCountedSet
:
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:array];
NSLog(@"Occurrences of Apple: %u", [countedSet countForObject:@"Apple"]);
为什么不干脆`出现次数+=[字符串IsequalString:@“苹果”]“?很抱歉,我花了很长时间才接受这个答案,但它看起来是最好的答案!谢谢!如果这是一个常见的操作,请使用NSCountedSet
。正如@Rob所提到的,在数组中循环,并建立一个NSMutableDictionary
,其中的值(如a
)作为键,该键的计数被视为值e、 这个答案很好。但是,您能从NSCountedSet获得具有重复成员的数组吗?(例如,用于将其存储在plist文件中)。现在我想到了一种转换NSCountedSet NSDictionary的方法(键是计数集中的对象,值是计数)会更好,因为plist会更小。我一直在谷歌搜索,找不到任何东西。可能我们需要手动实现这样一个方法。嗨,马丁,这个答案有一个额外的关闭参数,导致编译时错误。我试着编辑,但他们至少需要6个字符的编辑才能提交修复。行很好不过!
NSArray *myArray = [[NSArray alloc] initWithObjects:@"A", @"B", @"X", @"B", @"C", @"D", @"B", @"E", @"M", @"X", nil];
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:myArray];
for (NSString *item in countedSet) {
int count = [countedSet countForObject: item];
NSLog(@"the String ' %@ ' appears %d times in the array",item,count);
}
NSArray *array = [[NSArray alloc] initWithObjects:@"A", @"B", @"X", @"B", @"C", @"D", @"B", @"E", @"M", @"X", nil];
NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:array];
NSArray *uniqueStates = [[orderedSet set] allObjects];
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:array];
for(int i=0;i<[uniqueStates count];i++){
NSLog(@"%@ %d",[uniqueStates objectAtIndex:i], [countedSet countForObject: [uniqueStates objectAtIndex:i]]);
}
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:array];
NSLog(@"Occurrences of Apple: %u", [countedSet countForObject:@"Apple"]);