You can proceed similar as in this answer
to your previous question:
NSString *string1 = @"optimusprime";
NSString *string2 = @"dejathoras";
// Combine strings:
NSString *combined = [string1 stringByAppendingString:string2];
// Now remove duplicate characters:
NSMutableString *result = [combined mutableCopy];
[result enumerateSubstringsInRange:NSMakeRange(0, [result length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
// Check if current character already occurred at an earlier location:
NSRange previousRange = [result rangeOfString:substring
options:0
range:NSMakeRange(0, substringRange.location)];
if (previousRange.location != NSNotFound) {
[result deleteCharactersInRange:substringRange];
}
}];
NSLog(@"%@", result);
Output: optimusredjah
EDIT: The above solution does not scale very well. Storing duplicate characters
in a dictionary, as suggested in @waf’s answer, or in a set, is probably better:
NSMutableString *result = [combined mutableCopy];
NSMutableSet *chars = [NSMutableSet set];
[result enumerateSubstringsInRange:NSMakeRange(0, [result length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
if ([chars containsObject:substring]) {
[result deleteCharactersInRange:substringRange];
} else {
[chars addObject:substring];
}
}];
1
solved How to remove common letters in two Strings in iOS SDK? [duplicate]