数组去重,并把个数最多的那个元素放到数组最后面。 (今目标)
面试时候 思路有点乱。。回来重新写了个。
NSArray *array = @[@"1",@"3",@"2",@"1",@"3",@"4",@"5",@"2",@"3",@"1"];
NSInteger arraycount = array.count;
NSInteger testcount = 0;
NSInteger maxIndex = 0; 用来存放个数最多的下标
NSMutableArray *marray = [[NSMutableArray alloc]initWithArray:array];
for (int i = 0; i<arraycount; i++) {
NSInteger count = 1;
for (int j = i+1; j<arraycount; j++) {
if([marray[i] isEqualToString:marray[j]]){
[marray removeObjectAtIndex:j];
count++;
arraycount --;
}
}
if (count >testcount) {
maxIndex = i;
testcount = count;
}
}
[marray addObject:marray[maxIndex]];
[marray removeObjectAtIndex:maxIndex];
打印结果:
2016-09-30 15:24:41.175 block[22864:274969] (
3,
2,
4,
5,
1
)
如果有更简便的算法。。求指点。。