I have an array, as
(John, Jane, John)
I want to get duplicates,as well as original elements of array like
(John,John) I am able to get single occurance from code here
NSArray *names = [NSArray arrayWithObjects:@"John", @"Jane", @"John", nil];
NSCountedSet *set = [[NSCountedSet alloc] initWithArray:names];
for (id item in set)
{
NSLog(@"Name=%@, Count=%lu", item, (unsigned long)[set countForObject:item]);
if((unsigned long)[set countForObject:item]>1){
NSLog(@"of repeated element-----=%@",item);
}
}
"Name of repeated element-----John" but i want all occurences of repeated element like "Name of repeated element-----John,John" .
Try this Using NSPredicate:
NSArray *array = [NSArray arrayWithObjects:@"John", @"Jane", @"John",@"Jane",@"Jane", nil];
NSMutableArray *arrResult = [[NSMutableArray alloc] init];
NSCountedSet *set = [[NSCountedSet alloc] initWithArray:array];
for(id name in set)
{
if([set countForObject:name] > 1 ){
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF = %@", name];
[arrResult addObjectsFromArray:[array filteredArrayUsingPredicate:predicate]];
}
}
//
NSLog(@"%@",arrResult);