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 关注
- 396 浏览
添加回答
举报