1

I'm trying to get repeated characters in NSString with their count.

For example, setupsetpreset as an input and it returns 'set' along with the count 3.

Let me explain with another example.

Input 1 : upsetpreset

Expected output 1: set 2

Input 2 : qwertygdgpopgdg

Expected output 2: gdg 2

2 Answers2

2

Refer below

NSString *string     = @"This is a test. This is only a test.";
NSCountedSet *countedSet = [NSCountedSet new];

[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                       options:NSStringEnumerationByWords | NSStringEnumerationLocalized
                    usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){

                        // This block is called once for each word in the string.
                        [countedSet addObject:substring];

                        // If you want to ignore case, so that "this" and "This" 
                        // are counted the same, use this line instead to convert
                        // each word to lowercase first:
                        // [countedSet addObject:[substring lowercaseString]];
                    }];

NSLog(@"%@", countedSet);

Results:

"This" - 2 "is" - 2 "a" - 2 "test" - 2 "only" - 1

Awesome.Apple
  • 1,316
  • 1
  • 11
  • 24
1

After getting lot of custom logics to get common characters in a non-space string, I got the solution :-

NSString *string=@"setupsetpreset";
string=@"sheraheshehehesheshe";
NSMutableArray *a=[[NSMutableArray alloc] init];
NSMutableArray *b=[[NSMutableArray alloc] init];
for (int i=0; i<(string.length); i++) {
    [a addObject:[NSString stringWithFormat:@"%c",[string characterAtIndex:i]]];
}
NSCountedSet *countedSet = [NSCountedSet new];
[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                           options:NSStringEnumerationByComposedCharacterSequences
                        usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){
                            [countedSet addObject:substring];
                        }];
NSMutableArray *c=[[NSMutableArray alloc] init];
for (int i=0; i<(string.length); i++) {
    if([countedSet countForObject:[a objectAtIndex:i]]>=2){
        if(![b containsObject:[a objectAtIndex:i]]){
            [c addObject:[NSString stringWithFormat:@"%d",(int)[countedSet countForObject:[a objectAtIndex:i]]]];
        }
        [b addObject:[a objectAtIndex:i]];
    }
}

int total=0;
for (NSString *s in c){
    int m=[s intValue];
    total+=m;
}
total=total/c.count;
NSString *s=[b componentsJoinedByString:@""];
[c removeAllObjects];
for (int i=0; i<(s.length); i++) {
    if([countedSet countForObject:[b objectAtIndex:i]]>=total){

        [c addObject:[b objectAtIndex:i]];
    }
}
s=[c componentsJoinedByString:@""];
int iterate=0;
NSString  *substring;
NSMutableDictionary *dict=[[NSMutableDictionary alloc] init];
while (iterate<s.length-total) {
    NSString *searchstring=[s substringWithRange:NSMakeRange(iterate, total)];
    if ([string rangeOfString:searchstring].location == NSNotFound) {
    } else {
            [dict setObject:[NSNumber numberWithInt:[[dict objectForKey:searchstring] intValue]+1] forKey:searchstring];
        }
        iterate++;
    }
    [a removeAllObjects];
    [b removeAllObjects];
    a=[dict.allValues mutableCopy];
    b=[dict.allKeys mutableCopy];
    int max = [[a valueForKeyPath:@"@max.intValue"] intValue];
    NSInteger Aindex = [a indexOfObject:[NSNumber numberWithInt:max]];
    substring =[NSString stringWithFormat:@"%@",[b objectAtIndex:Aindex]];
    NSLog(@"Substring %@ is repeated %d times",substring,max);

Hope it helps for someone's need :) Thanks Kiran for providing me a set of logic which is required as a part and Ruchira for your query.