该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
5、从一个数组中找出重复的元素打印出来
NSArray *arr = [NSArrayarrayWithObjects:@"1",@"2",@"1",@"7",@"4",@"5",@"2",@"6",@"5",nil];
NSMutableArray *arrmu = [[NSMutableArrayalloc]init];//过滤
NSMutableArray *sameArray = [[NSMutableArrayalloc]init];//找出相同的
for (int i = 0 ; i < [arrcount]; i++) {
id str = [arr objectAtIndex:i];
if ([arrmu count] == 0)
{
[arrmuaddObject:str];
}
else{
BOOL flag = NO;
for (int j = 0; j < [arrmucount]; j++ ) {
if ([strisEqual:[arrmu objectAtIndex:j]])
{
[sameArrayaddObject:str];
flag =YES;
break;
}
else{
flag =NO;
}
}
if (flag == NO) {
[ arrmuaddObject:str];
}
}
}
NSLog(@"sameArray : %@",sameArray);
还有两种直接找出的方法,上代码:
一:
NSArray *arr = [NSArrayarrayWithObjects:@"1",@"2",@"1",nil];
NSSet *set = [NSSetsetWithArray:arr];
NSLog(@"%@",[setallObjects]);
二:
NSArray *arr =@[@1,@2,@1];
NSMutableDictionary *dict = [NSMutableDictionarydictionary];
for (NSNumber *numberin arr) {
[dictsetObject:numberforKey:number];
}
NSLog(@"%@",[dictallValues]);