.h文件
@interface NSMutableArray (FilterElement)
/**
* 过滤掉相同的元素
*
* @return 返回一个数组
*/
- (NSMutableArray*)filterTheSameElement;
@end
.m文件
@implementation NSMutableArray (FilterElement)
/**
* 过滤掉相同的元素
*
* @return 返回一个数组
*/
- (NSMutableArray*)filterTheSameElement
{
NSMutableSet *set = [NSMutableSet set];
for (NSObject *obj in self) {
[set addObject:obj];
}
[self removeAllObjects];
for (NSObject *obj in set) {
[self addObject:obj];
}
return self;
}
@end
网友评论