How can I remove duplicated substings from string? For ex. I have: aaa,bbb,ttt,bbb,rrr. And in result I want to have aaa,bbb,ttt,rrr (deleted duplicated bbb). I hope for your help. Thanks.
NSString remove duplicated substrings
1k Views Asked by LightNight At
3
There are 3 best solutions below
0

You can use NSMutableDictionary; In dictionary there are two elements; 1. Key 2. Value
Just set Keys as your array elements; Special point is that 'Key' can't be duplicate;
Now just get array of Keys by using [dictionary allKeys];
Now, at this stage you have unique values in new array;
0

You can do it like this:
NSMutableSet *seen = [NSMutableSet set];
NSMutableString *buf = [NSMutableString string];
for (NSString *s in [str componentsSeparatedByString:@","]) {
if (![seen containsObject:s]) {
[seen add:s];
[buf appendFormat:@",%@", s];
}
}
NSString *res = [buf length] ? [buf substringFromIndex:1] : @"";
Do it in three steps
1)
NSArray *items = [theString componentsSeparatedByString:@","];
2) remove duplicate element from array
3) Concate String from array