0

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
4

1 回答 1

11

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]);
}
于 2013-03-30T22:57:33.743 回答