nsmutablearray - Objective-C: Finding the amount of times an Object occurs in an Array with indexes -
this question has answer here:
i need way of counting how many time object occurs in given mutablearray , returning indexes of objects seperate mutable array. ive tried doing couple of ways cant figure out.
so have array containing 2,3,3,4,3,5,3 when searching 3, should give me both number of times, 4, , seperate array containing 1,2,4,6 (the indexes of objects.
i saw following code here on site, cant work out how modify it, can me?
int occurrences = 0; for(nsstring *string in array){ occurrences += ([string isequaltostring:@"apple"]?1:0); //certain object @"apple" }
int occurrences = 0; nsmutablearray *indices = [nsmutablearray array]; int = 0; (i = 0; < [array count]; i++) { nsstring *obj = [array objectatindex:i]; if ([obj isequaltostring:@"apple"]) { occurrences++; [indices addobject:[nsnumber numberwithint:i]]; } }
you drop tracking occurrences
, like
int occurrences = [indices count];
after for
loop.
Comments
Post a Comment