2012-05-25 40 views
0

如何知道數組中出現重複的次數?在NSMutableArray中查找重複值

for (NSUInteger index = 0; index < count; index++) 

{ 

    NSDictionary *dico = [myArray objectAtIndex:index ]; 

    NSString *exp = [dico objectForKey:@"name"]; 

       NSLog(@"index %d : %@",index,exp); 
    } 

這是我的NSLog:

index 0 : Mike 
index 1 : Peter 
index 2 : Franck 
index 3 : Peter 

想知道哪裏是我的重複值。

THX

+0

你想消除重複或只是算一個名字出現了多少次在你的數組? – Alladinian

+0

嗨想要刪除它們,但需要保留一個 – XcodeMania

回答

3

如果我沒看錯的NSSet的目的是要做到這一點... 試試這個..

NSSet *uniqueElements = [NSSet setWithArray:myArray]; 

for(id element in uniqueElements) { 
    // iterate here 
} 
+0

'for(id元素在[NSSet setWithArray:myArray]){}' – Richard

+0

同樣的事情......只是想讓它對用戶更加清楚... –