在iOS中从阵列获取重复项和原始项 [英] To Get Duplicate as well as original items from an array in iOS
本文介绍了在iOS中从阵列获取重复项和原始项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组,例如
(约翰,简,约翰)
我想得到重复的像数组一样的原始元素
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);
}
}
重复元素的名称----- John",但我希望出现所有重复元素,例如重复元素的名称----- John,John".
"Name of repeated element-----John" but i want all occurences of repeated element like "Name of repeated element-----John,John" .
推荐答案
尝试此使用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);
这篇关于在iOS中从阵列获取重复项和原始项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文