NSArray:删除具有重复属性的对象 [英] NSArray: Remove objects with duplicate properties
问题描述
我有一个包含一些自定义对象的NSMutableArray。其中两个对象具有相同的属性,例如标题和作者。我想删除重复的对象并离开另一个。
I have an NSMutableArray that contains a few custom objects. Two of the objects have the same properties such as title and author. I want to remove the duplicate object and leave the other.
Asset *asset;
NSMutableArray *items = [[[NSMutableArray alloc] init] autorelease];
// First
asset = [[Asset alloc] init];
asset.title = @"Developer";
asset.author = @"John Smith";
[items addObject:asset];
[asset release];
// Second
asset = [[Asset alloc] init];
asset.title = @"Writer";
asset.author = @"Steve Johnson";
[items addObject:asset];
[asset release];
// Third
asset = [[Asset alloc] init];
asset.title = @"Developer";
asset.author = @"John Smith";
[items addObject:asset];
[asset release];
由于它们不是同一个对象,但只有重复的属性,我该如何删除重复?
推荐答案
你可以创建一个HashSet,当你循环时,可以添加title + author连接设置为HashSet(NSMutableSet)。当您到达每个项目时,如果HashSet包含您的密钥,请删除它或不复制(删除或创建没有重复的副本)。
You could create a HashSet and as you loop, you could add "title+author" concatenated set to the HashSet (NSMutableSet). As you arrive at each item, if the HashSet contains your key, either remove it or don't copy (either deleting or creating a copy without duplicates).
这使得它命令n(1循环)
That makes it order n (1 loop)
这是NSMutableSet类:
Here's the NSMutableSet class:
使用代码进行编辑:
代码的内容是一个循环。
The meat of the code is the one loop.
void print(NSMutableArray *assets)
{
for (Asset *asset in assets)
{
NSLog(@"%@/%@", [asset title], [asset author]);
}
}
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//
// Create the initial data set
//
Asset *asset;
NSMutableArray *items = [[[NSMutableArray alloc] init] autorelease];
// First
asset = [[Asset alloc] init];
asset.title = @"Developer";
asset.author = @"John Smith";
[items addObject:asset];
[asset release];
// Second
asset = [[Asset alloc] init];
asset.title = @"Writer";
asset.author = @"Steve Johnson";
[items addObject:asset];
[asset release];
// Third
asset = [[Asset alloc] init];
asset.title = @"Developer";
asset.author = @"John Smith";
[items addObject:asset];
[asset release];
NSLog(@"****Original****");
print(items);
//
// filter the data set in one pass
//
NSMutableSet *lookup = [[NSMutableSet alloc] init];
for (int index = 0; index < [items count]; index++)
{
Asset *curr = [items objectAtIndex:index];
NSString *identifier = [NSString stringWithFormat:@"%@/%@", [curr title], [curr author]];
// this is very fast constant time lookup in a hash table
if ([lookup containsObject:identifier])
{
NSLog(@"item already exists. removing: %@ at index %d", identifier, index);
[items removeObjectAtIndex:index];
}
else
{
NSLog(@"distinct item. keeping %@ at index %d", identifier, index);
[lookup addObject:identifier];
}
}
NSLog(@"****Filtered****");
print(items);
[pool drain];
return 0;
}
这是输出:
Craplet[11991:707] ****Original****
Craplet[11991:707] Developer/John Smith
Craplet[11991:707] Writer/Steve Johnson
Craplet[11991:707] Developer/John Smith
Craplet[11991:707] distinct item. keeping Developer/John Smith at index 0
Craplet[11991:707] distinct item. keeping Writer/Steve Johnson at index 1
Craplet[11991:707] item already exists. removing: Developer/John Smith at index 2
Craplet[11991:707] ****Filtered****
Craplet[11991:707] Developer/John Smith
Craplet[11991:707] Writer/Steve Johnson
这篇关于NSArray:删除具有重复属性的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!