标签:
NSArray *array = @[@"小玉",@"小小鱼",@"小玉"]; //数组去重方法---方法1 //建立一个新的的可变数组,数组长度为0 NSMutableArray *newArray = [NSMutableArray array]; //遍历array,然后判断新数组中是否包含该元素,如果没有,则添加。 for (NSString *string in array) { if (![newArray containsObject:string]) { [newArray addObject:string]; } } //输出新数组中的元素 for (NSString *string in newArray) { NSLog(@"%@",string); }
NSArray *array = @[@"小玉",@"小小鱼",@"小玉"];
//字符串去重方法二 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; //根据字典的key值是唯一的,来写入数组 for (NSNumber *number in array) { //把number拼接成字符串 [dict setValue:number forKey:[NSString stringWithFormat:@"%@",number]]; } NSLog(@"%@",[dict allValues]);
NSArray *array = @[@"小玉",@"小小鱼",@"小玉"]; //第三种去重方法NSSet NSSet *set = [[NSSet alloc]initWithArray:array]; NSLog(@"%@",[set allObjects]);
NSArray *array = @[@"小玉",@"小小鱼",@"小玉"]; //数组有序排列 NSOrderedSet *orderSet = [NSOrderedSet orderedSetWithArray:array]; NSLog(@"%@",orderSet.array);
标签:
原文地址:http://www.cnblogs.com/lishishi/p/5495027.html