29,027
社区成员
发帖
与我相关
我的任务
分享
#define format(fmtString, args...) [NSString stringWithFormat:fmtString, args]
NSArray *arr = @[@"aa", @"bb", @"cc", @"dd", @"aa", @"dd", @"cc", @"aa"];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (NSString *value in arr)
{
[dict setObject:@"-1" forKey:value];
}
NSMutableArray *finalArray = [NSMutableArray array];
for (NSString *value in arr)
{
for (NSString *key in dict)
{
if ([value isEqualToString:key])
break;
}
dict[value] = format(@"%d", [dict[value] intValue]+1);
NSString *obj = (![dict[value] intValue]) ? format(@"%@", value) : format(@"%@%d", value, [dict[value] intValue]);
[finalArray addObject:obj];
}
NSLog(@"%@", finalArray);