views:

67

answers:

2

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≥.

+1  A: 

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);
Georg Fritzsche
A: 

If you want only to remove duplicates, it is very easy, use NSSet

NSSet set = [NSSet setWithArray:dupArray];

Guy Ephraim
no,i dont want to remove duplicates ........i only want indexes of duplicates..