How to check that how many time items are repeated in array. I am having an array that contains repetitive item. Below is array.
"Family:0",
"Family:0",
"Family:0",
"Gold:3",
"Gold:3"
So, i want in response values 3 and 2 for respective items. How can i achieve that. Hope i made my point clear. If any thing not clear please ask.
Below is my code i tried.
int occurrences = 0;
int i=0;
for(NSString *string in arrTotRows){
occurrences += ([string isEqualToString:[arrTotRows objectAtIndex:indexPath.section]]); //certain object is @"Apple"
i++;
}
You can use NSCountedSet for this. Add all your objects to a counted set, then use the countForObject:
method to find out how often each object appears.
Example
NSArray *names = [NSArray arrayWithObjects:@"Family:0", @"Family:0", @"Gold:3", @"Gold:3", nil];
NSCountedSet *set = [[NSCountedSet alloc] initWithArray:names];
for (id item in set)
{
NSLog(@"Name=%@, Count=%lu", item, (unsigned long)[set countForObject:item]);
}
Output
Name=Gold:3, Count=2
Name=Family:0, Count=2