Reputation: 13
i have an array like
[chapter,indent,left,indent,nonindent,chapter,chapter,indent,indent,left];
i need to find indexes of duplicates and also non duplicate elements . how to do this...........give some sample code or logic...... thanks in advance
iam using objective 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];
this is my sample code...in this the array tempo contains all objects from that array i want to get index of duplicate strings≥.
Upvotes: 0
Views: 1346
Reputation: 99044
You could build a dictionary mapping the objects to index sets. For every index set, a -count
of 1
means no duplicates, > 1
means there are duplicates.
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);
Upvotes: 2