2010-09-21 87 views
0

我有一個像查找重複的指數在NSArray中

[chapter,indent,left,indent,nonindent,chapter,chapter,indent,indent,left]; 

我需要找到重複的指標,也非重複元素的數組。 如何做到這一點...........給一些示例代碼或邏輯......使用Objective C的 由於事先

IAM .....

NSArray *myWords = [string componentsSeparatedByString:@"class=\""]; 

    int count_var=[myWords count]; 


    [email protected]""; 
    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]; 

這是我的示例代碼...在這個數組速度包含該數組中的所有對象我想獲取重複字符串≥的索引。

+0

iam using objective c – user452815 2010-09-21 05:42:31

+0

我不明白你是什麼意思,說實話,即使看了樣本代碼。你的數組的格式是什麼?是章節,縮進,非縮進是字符串還是變量? – vodkhang 2010-09-21 06:04:04

回答

2

您可以建立一個將對象映射到索引集的字典。對於每個索引集,-count1表示不重複,> 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);