3 回答

TA貢獻1821條經驗 獲得超6個贊
NSArray
如果你不關心命令.
uniquearray = [yourarray valueForKeyPath:@"@distinctUnionOfObjects.self"];
@distinctUnionOfObjects.name

TA貢獻1804條經驗 獲得超3個贊
// Initialise a new, empty mutable array NSMutableArray *unique = [NSMutableArray array];for (id obj in originalArray) { if (![unique containsObject:obj]) { [unique addObject:obj]; }}
containsObject:
indexOfObject:inRange:
NSMutableSet
NSMutableArray *unique = [NSMutableArray array];NSMutableSet *seen = [NSMutableSet set];for (id obj in originalArray) { if (![seen containsObject:obj]) { [unique addObject:obj]; [seen addObject:obj]; }}
NSMutableSet *seen = [NSMutableSet set];NSUInteger i = 0;while (i < [originalArray count]) { id obj = [originalArray objectAtIndex:i]; if ([seen containsObject:obj]) { [originalArray removeObjectAtIndex:i]; // NB: we *don't* increment i here; since // we've removed the object previously at // index i, [originalArray objectAtIndex:i] // now points to the next object in the array. } else { [seen addObject:obj]; i++; }}
更新removeObjectAtIndex:
(他說“可能”是因為我們不確定它是如何實現的;但一個可能的實現是,在刪除索引X處的對象之后,該方法循環遍歷從索引X+1到數組中的最后一個對象的每個元素,然后將它們移動到前一個索引。如果是這樣的話,那就是O(N)的性能。
- 3 回答
- 0 關注
- 429 瀏覽
添加回答
舉報