我有一个像
这样的数组[chapter,indent,left,indent,nonindent,chapter,chapter,indent,indent,left];
我需要查找重复索引以及非重复元素。 怎么做...........给出一些示例代码或逻辑...... 提前谢谢
我使用目标c .....
NSArray *myWords = [string componentsSeparatedByString:@"class=\""];
int count_var=[myWords count];
tmp1=@"";
for(int i=1;i<count_var;i++)
{
str=[NSString stringWithFormat:@"\n%@",[myWords objectAtIndex:i]];
class=[str componentsSeparatedByString:@"\""];
NSString *tmp=[NSString stringWithFormat:@"%@",[class objectAtIndex:0]];
tmp1=[[NSString stringWithFormat:@"%@",tmp1] stringByAppendingString:[NSString stringWithFormat:@"%@",tmp]];
}
t1.editable=NO;
t1.text=tmp1;
NSArray *tempo=[[NSArray alloc]init];
tempo=[tmp1 componentsSeparatedByString:@"\n"];
tempCount=[tempo count];
这是我的示例代码...在此,数组tempo包含来自该数组的所有对象,我想获得重复字符串的索引≥。
答案 0 :(得分:2)
您可以构建一个将对象映射到索引集的字典。对于每个索引集,-count
1
表示没有重复,> 1
表示存在重复。
NSArray *arr = [NSArray arrayWithObjects:...];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (NSUInteger i=0; i<[arr count]; ++i) {
id obj = [arr objectAtIndex:i];
NSMutableIndexSet *ids = [dict objectForKey:obj];
if (!ids) {
ids = [NSMutableIndexSet indexSet];
[dict setObject:ids forKey:obj];
}
[ids addIndex:i];
}
NSLog(@"%@", dict);