objective-c - 检查 Objective-C 中相同字符的字符串

标签 objective-c nscharacterset

我有一个字符串数组,我只想从中提取具有唯一字符集的字符串。 (例如,“asdf”和“fdsa”将被认为是多余的)。这是我目前使用的方法:

NSMutableArray *uniqueCharSets = [[NSMutableArray alloc] init];
NSMutableArray *uniqueStrings = [[NSMutableArray alloc] init];        

for (NSString *_string in unique) {
    NSCharacterSet *_charSet = [NSCharacterSet characterSetWithCharactersInString:_string];
    if (![uniqueCharSets containsObject:_charSet]) {
        [uniqueStrings addobject:_string];
        [uniqueCharSets addObject:_charSet];
    }
}

这似乎可行,但速度非常慢且占用大量资源。谁能想到更好的方法来做到这一点?

最佳答案

  1. 使用 NSDictionary,将每个字符串的字典顺序映射到输入字符串的 NSArray:(例如 adfs => [afsd, asdf, ...])
  2. 遍历字典,打印出只有单元素数组值的键(或它们的值)

关于objective-c - 检查 Objective-C 中相同字符的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8696126/

相关文章:

objective-c - stringByTrimmingCharactersInSet : is not removing characters in the middle of the string

ios - 未知对象类型的zlib解压方法

iphone - 在 SpriteKit 游戏中计算速度

ios - UINavigationController 做事务时发生崩溃

iphone - NSCharacter Set 使用 int,但我需要未分配的短字符?

swift - 将大型混合数字和文本数字转换为 Swift 或 Objective-C 中的数字

iphone - 可达性最佳实践

ios - Objective-C KVC 改变属性的类型

swift - 将字符集转换为字符串或数组或字符集

swift - 如何知道哪个CharacterSet包含给定的字符?