How can I save the string that match from one NSArray with one index difference in NSMutableArray?
For example, there are three “apple”, four “pineapple”, six “banana”, two “cocoa” and the rest of words dont have duplicate(s) in the nsarray, i would like to know if the nsarray has at least two same words. If yes, I would like to save “apple”, “pineapple, “banana” and “cocoa” once in nsmutablearray. If there are other alike words, I would like to add them to namutablearray too.
My code (which still doesn’t work properly);
NSArray *noWords = [[NSArray alloc] initWithArray:
[[NSString stringWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:@"words" ofType:@"txt"]
encoding:NSUTF8StringEncoding error:NULL]
componentsSeparatedByString:@"\n"]];
NSUInteger scount = [noWords count];
int ii = 0;
NSString *stringline;
for (ii; ii < scount; ii++)
{
stringline = [noWords objectAtIndex:ii];
NSLog(@"stringline : %@ ", stringline);
}
int i = 1;
NSString *line;
for (i ; i < 10; i++)
{
line = [noWords objectAtIndex:i];
NSLog (@"line : %@ ", line);
NSMutableArray *douwords = [NSMutableArray array];
if ([stringline isEqualToString:line])
{
NSString *newword;
for (newword in douwords)
{
[douwords addObject:newword];
NSLog (@"detected! %@ ", douwords);
}
}
}
I assume that you want to count appearances of words in your array and output those with a count of more than one. A basic and verbose way to do that would be:
The output would be: