从字符数组中去重复

NSArray *array = @[@"12-11", @"12-11", @"12-11", @"12-12", @"12-13", @"12-14"];
//方法1 重新创建一个可变数组,把数组的值一个个往可变数组加,如果不存在就加,存在就不加
NSMutableArray *copyArray = [NSMutableArray array];
for (NSString *str in array)
{
if( NO == [copyArray containsObject:str])
{
[copyArray addObject:str];
}
}
//方法2 利用字典对应的键值对,相同键值会覆盖前面的,比数组简单,不用去做判断
NSMutableDictionary *copyDic = [NSMutableDictionary dictionary];
for (NSString *str in array)
{
[copyDic setObject:str forKey:str];
}
NSArray *array2 = [copyDic allValues];

你可能感兴趣的:(从字符数组中去重复)