문제

This is a core function question. I always dealt with this problem on the database side, by I can't do it for this case and I am stumped. I want to count the distinct values. Let's say I have an array

Array = {a,a,a,a,b,b,b,b,c,c,c,c}

And what I want is simply to get

Result = {[a,4],[b,4],[c,4]}  or

ResultDistinct = { a, b, c} , ResultCount = {4,4,4}

I am fine with whatever format as long as it is fast and neat.

도움이 되었습니까?

해결책

Use NSCountedSet.

NSArray *myArray = ... // array with all the a, b, and c values
NSCountedSet *set = [[NSCountedSet alloc] initWithArray:myArray];
for (id obj in set) {
    NSLog(@"There are %d instances of %@", [set countForObject:obj], obj);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top