cmii
cmii

Reputation: 3636

How many occurrence of each item appears in a NSMutableArray

I have an NSMutableArray with some NSNumber :

{1, 2, 3, 3, 2, 1, 6, 2}

I would like to know how many occurrence of each number appears in the list.

Ex :  
1 = 2  
2 = 3  
3 = 2  
6 = 1

Upvotes: 0

Views: 685

Answers (1)

Sulthan
Sulthan

Reputation: 130200

You can put all the items into a NSCountedSet.

NSCountedSet* countedSet = [[NSCountedSet alloc] initWithArray:array];

for (NSNumber* number in countedSet) {
   NSLog(@"%@ = %u", number, [countedSet countForObject:number]);
}

Upvotes: 11

Related Questions