Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check strings for same characters in Objective-C

I have an array of strings, from which I would like to extract only those with unique character sets. (For example, "asdf" and "fdsa" would be considered redundant). This is the method I am currently using:

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];
    }
}

This seems to work, but it's very slow and resource-intensive. Can anyone think of a better way to do this?

like image 401
serverpunk Avatar asked Jan 01 '12 22:01

serverpunk


1 Answers

  1. Using an NSDictionary, map each string's lexicographically-sorted equivalent to an NSArray of input strings: (e.g. adfs => [afsd, asdf, ...])
  2. Walk through the dictionary, printing out keys (or their values) which only have single-element array values
like image 81
Alex Reynolds Avatar answered Oct 04 '22 20:10

Alex Reynolds