在NSArray中查找重复项 [英] Find duplicates in NSArray
本文介绍了在NSArray中查找重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设您有一个具有重复项 @ [1,2,3,1,1,2,4,5,6]的
NSArray
/ code>;
Say you have an NSArray
with duplicates @[1,2,3,1,1,2,4,5,6]
;
查找所有重复项;这可以是伪代码。这是一个算法问题,而不是一个基础框架(没有使用 NSSet
)的问题。
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.
推荐答案
如@Lithu所述,使用 NSCountedSet
,请参阅下面的代码。
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);
}
这篇关于在NSArray中查找重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文