Monday, 9 September 2013

Creating NSArray of repeated numbers

Creating NSArray of repeated numbers

I want to initialize NSArray with given numbers of zeros. I need it to
create an array of counters and in the end of the function return an index
of the counter with highest value.
So the question is how to initialize NSArray of zeros of given length? Is
there a good way to do it in 1-2 lines? Maybe there is a better way to
count objects? How would you do it?
Thanks!
UPD: Here is the code of my existing function. It returns the maximum
number of cards, which have the same rank.
+ (int)ranksMatched:(NSArray *)cards
{
NSMutableArray *ranksCounter = [[NSMutableArray alloc]
initWithCapacity:[PlayingCard maxRank]];
for (int i = 0; i < [PlayingCard maxRank]; ++i) {
[ranksCounter addObject:[NSNumber numberWithInt:0]];
}
int maxIndex = 0;
int maxValue = 0;
for (PlayingCard *card in cards) {
int currentValue = [ranksCounter[card.rank] intValue] + 1;
if (maxValue < currentValue) {
maxValue = currentValue;
maxIndex = card.rank;
}
}
return maxIndex;
}

No comments:

Post a Comment