开发者

Objective-C: Count the number of times an object occurs in an array?

开发者 https://www.devze.com 2023-02-07 21:44 出处:网络
I need to perform what I feel is a basic function but I can\'t find any documentation on how to do it. Please help!

I need to perform what I feel is a basic function but I can't find any documentation on how to do it. Please help!

I need to count how man开发者_Python百科y times a certain object occurs in an array. See example:

array = NSArray arrayWithObjects:@"Apple", @"Banana", @"Cantaloupe", @"Apple", @"DragonFruit", @"Eggplant", @"Apple", @"Apple", @"Guava",nil]retain];

How can I iterate through the array and count the number of times it finds the string @"Apple"?

Any help is appreciated!


One more solution, using blocks (working example):

NSInteger occurrences = [[array indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {return [obj isEqual:@"Apple"];}] count];
NSLog(@"%d",occurrences);


As @bbum said, use an NSCounted set. There is an initializer thet will convert an array directly into a counted set:

    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 output: (D [1], M [1], E [1], A [1], B [3], X [2], C [1])

Just access items:

count = [countedSet countForObject: anObj]; ...


A Simple and specific answer:

int occurrences = 0;
for(NSString *string in array){
    occurrences += ([string isEqualToString:@"Apple"]?1:0); //certain object is @"Apple"
}
NSLog(@"number of occurences %d", occurrences);

PS: Martin Babacaev's answer is quite good too. Iteration is faster with blocks but in this specific case with so few elements I guess there is no apparent gain. I would use that though :)


Use an NSCountedSet; it'll be faster than a dictionary and is designed to solve exactly that problem.

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


Just came across this pretty old question. I'd recommend using a NSCountedSet:

NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:array];
NSLog(@"Occurrences of Apple: %u", [countedSet countForObject:@"Apple"]);


I would encourage you to put them into a Dictionary (Objective C's version of a map). The key to the dictionary is the object and the value should be the count. It should be a MutableDictionary of course. If the item is not found, add it and set the count to 1.


- (int) numberOfOccurrencesForString:(NSString*)needle inArray:(NSArray*)haystack {
    int count = 0;

    for(NSString *str in haystack) {
        if([str isEqualToString:needle]) {
            count++;
        }
    }

    return count;
}


I up-voted Rob's answer, but I wanted to add some code that I hope will be of some assistance.

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


If the array is sorted as in the problem statement then you don't need to use a dictionary.

You can find the number of unique elements more efficiently by just doing 1 linear sweep and incrementing a counter when you see 2 consecutive elements being the same.

The dictionary solution is O(nlog(n)), while the linear solution is O(n).

Here's some pseudo-code for the linear solution:

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


the complete code with reference to @bbum and @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);
}

Thank you.


If you want it more generic, or you want to count equals/different objects in array, try this:

Sign "!" count DIFFERENT values. If you want SAME values, remove "!"

  int count = 0;
  NSString *wordToCheck = [NSString string];
  for (NSString *str in myArray) {
    if( ![str isEqualToString:wordToCheck] ) {
      wordToCheck = str;
      count++;
    }
  }

hope this helps the community!

I've used it to add correct number of sections in uitableview!


You can do this way,

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

The result is like : A 1

0

精彩评论

暂无评论...
验证码 换一张
取 消