Say you have an NSArray
with duplicates @[1,2,3,1,1,2,4,5,6]
;
Find all the duplicates; this can be in pseudocode. This is more of a algorithm question than a Foundation framework (without the use of NSSet
) question.
as @Lithu described, use NSCountedSet
, see the below code.
NSArray *arr = [[NSArray alloc]initWithObjects:@(1),@(1),@(2), @(1),nil];
NSCountedSet *cs = [[NSCountedSet alloc] initWithArray:arr];
NSLog(@"object count greater than 1 are");
for(NSNumber *num in cs)
{
if([cs countForObject:num]>1)
NSLog(@"%@",num);
}
Use an NSCountedSet
and only print the elements that returns a number>1 for countForObject:
method
Refer this for more information
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With