Here is the code for grouping and printing the set of anagrams in an array.If any better solutions please Comment.


- (void)viewDidLoad
 {
 [super viewDidLoad];
 //self.view.backgroundColor=[UIColor blueColor];
 // Do any additional setup after loading the view, typically from a nib.
 NSString *tmpStr1;
 NSString *tmpStr2;
 NSMutableArray *newArr=[[NSMutableArray alloc] init];
 BOOL Var=NO;
 int i,j;
 NSArray *arr1=[[NSArray alloc] initWithObjects:@"star",
 @"rats",
 @"arts",
 @"car",
 @"rac",
 @"arc",
 @"abcde",
 @"bcdea",
 @"cdeab", nil];
 for (i=0; i<[arr1 count]; i++) {
 for (j=i+1; j<[arr1 count]; j++) {
 tmpStr1=[arr1 objectAtIndex: i]; //star
 tmpStr2=[arr1 objectAtIndex:j]; //rats
 if ( tmpStr1.length == tmpStr2.length) { //4==4
 [newArr addObject:tmpStr1]; //newArray=[star]
 for (int k=0; k<tmpStr2.length; k++) {
 //Get the letter by letter in tmpStr2
 NSString cmpStr=[NSString stringWithFormat:@"%C",[tmpStr2 characterAtIndex:k]]];
 if ([tmpStr1 rangeOfString:cmpStr.location == NSNotFound) {
 Var=NO; //Characters in tmpStr2 not found in tmpStr1
 } else {
 Var=YES; //Characters in tmpStr2 found in tmpStr1
 }
 }
 //if all the letters in string tmpStr2 are present in string tmpStr1
 if (Var) { 
 //add the second string i.e. tmpStr2 to the array newArr
 [newArr addObject:tmpStr2]; 
 }
 }
 else
 {
 i=j; 
 //remove the duplicate strings in the array newArr
 //print the anagram set to log
 NSLog(@"NewArr::>%@",[[NSSet setWithArray:newArr] allObjects]); 
 //remove the objects in the array for the next set of anagrams
 [newArr removeAllObjects]; 
 //Controll goes to this line::> for (j=i+1; j<[arr1 count]; j++)
 continue; 
 }
 }
 }
 NSLog(@"Arr2::%@",[[NSSet setWithArray:newArr] allObjects]);
 NSLog(@"Arr1::%@",arr1); //To print the original array
 }



Find on Git: Here!