今天遇到一个需求,一个数组中有一些元素,当然是什么元素就不知道了。总之,不能让数组中的元素重复。
一开始想到的方法是遍历这个数组,一个一个的查找。但是总感觉这样太繁琐,于是,找了点资料总结了以下两种解决办法
NSArray *arr = @[@111,@222,@111];
NSMutableDictionary *dict = [NSMutableDictionarydictionary];
for (NSNumber *number in arr) {
[dict setObject:number forKey:number];
}
NSLog(@"%@",[dict allValues]);
输出结果为:
2013-05-21 12:03:49.449 test1[4377:c07] (
111,
222
)
NSArray *arr = @[@111,@222,@111];
NSSet *set = [NSSet setWithArray:arr];
NSLog(@"%@",[set allObjects]);
输出结果为:
2013-05-21 12:06:26.508 test1[4547:c07] (
111,
222
)

762

被折叠的 条评论
为什么被折叠?



