Check duplicate property values of objects in NSArray

寵の児 提交于 2019-12-01 12:33:47
Amar

Try this code:

NSSet *myset = [NSSet setWithArray:[myarray valueForKey:@"size"]];
int duplicatesCount = [myarray count] - [myset count];

size here is the object property.

Nishant Tyagi

Use NSCountedSet. then add all your objects to the counted set, and use the countForObject: method to find out how often each object appears in your array.

You can check this link also how-to-find-duplicate-values-in-arrays

Hope it helps you

Probably simplest is to sort the array based on the size field and then step through the sorted list looking for adjacent dupes.

You could also "wrap" each object in one that exports the size as its key and use a set. But that's a lot of extra allocations.

But if you only want to know if dupes exist, and not which ones they are, create an NSNumber for each object's size and insert the NSNumbers in a set. The final size will tell you how many dupes.

NSArray *cleanedArray = [[NSSet setWithArray:yourArraywithDuplicatesObjects ] allObjects];

Use Sets this will remove all duplicates objects.Will return NSArrayNSCountedSet and use countForObject: method to find out how often each object appears how many times.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!