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.