第一種:利用NSDictionary的AllKeys(AllValues)方法
NSArray *dataArray = @[@1, @2, @3, @4, @1, @2, @5, @6, @7, @8, @9, @1, @3];
NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithCapacity:0];
for(NSString *str in dataArray)
{
[dic setValue:str forKey:str];
}
NSLog(@"DIC %@",[dic allKeys]);
打印結(jié)果:
DIC (
9,
5,
1,
6,
2,
7,
3,
8,
4
)
第二種方法:利用NSSet的AllObjects方法
NSArray *dataArray = @[@1, @2, @3, @4, @1, @2, @5, @6, @7, @8, @9, @1, @3];
NSSet *set = [NSSet setWithArray:dataArray];
NSLog(@"SET = %@",[set allObjects]);
SET = (
9,
7,
5,
3,
1,
8,
6,
4,
2
)
第三種方法,利用數(shù)組的containsObject來去除
NSArray *dataArray = @[@1, @2, @3, @4, @1, @2, @5, @6, @7, @8, @9, @1, @3];
NSMutableArray *listAry = [[NSMutableArray alloc]init];
for (NSString *str in dataArray) {
if (![listAry containsObject:str]) {
[listAry addObject:str];
}
}
NSLog(@"CONTAINS %@",listAry);
打印結(jié)果:
CONTAINS (
1,
2,
3,
4,
5,
6,
7,
8,
9
)
第四種方法:利用keyValue的方式(自己不是太明白)
NSArray *dataArray = @[@1, @2, @3, @4, @1, @2, @5, @6, @7, @8, @9, @1, @3];
NSArray *array1 = [dataArray valueForKeyPath:@"@distinctUnionOfObjects.self"];
NSLog(@"DISTINC %@",array1);
打印結(jié)果
DISTINC (
9,
7,
5,
3,
1,
8,
6,
4,
2
)
第五種 : NSOrderedSet去除
NSArray *dataArray = @[@1, @2, @3, @4, @1, @2, @5, @6, @7, @8, @9, @1, @3];
NSOrderedSet *orderSet = [NSOrderedSet orderedSetWithArray:dataArray];
NSArray *newArray = orderSet.array;
NSLog(@"ORDERSET %@",newArray);
打印結(jié)果
ORDERSET (
1,
2,
3,
4,
5,
6,
7,
8,
9
)