Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get duplicates in NSArray

I have an NSArray which contains a few duplicate objects. I want to print which objects are getting duplicated, for example:

NSArray * array = [NSArray arrayWithObjects: A, B, C, A, B];

Now I want to print in my console A & B as these are duplicated.

How do I do this?

like image 788
hgpl Avatar asked Dec 01 '22 21:12

hgpl


2 Answers

You can use NSCountedSet for this. you can add all the objects in a counted set, then use the countForObject: method to find out how often each object appears. read about NSCountedSet for further reference

like image 96
nsgulliver Avatar answered Dec 21 '22 11:12

nsgulliver


Use an NSCountedSet and only print the elements that returns a number>1 for countForObject: method

like image 27
Kaan Dedeoglu Avatar answered Dec 21 '22 11:12

Kaan Dedeoglu