The most naive approach (well not counting yours) is something like this:
// Replace newString if found, insert new into top
int match = -1;
int index = 0;
for(NSString * string in historyArray)
{
if([newString isEqualToString:string])
{
match = index;
break;
}
index++;
}
if(match != -1)
[historyArray removeObjectAtIndex:index];
[historyArray insertObject:newString atIndex:0];
If you then want to limit the size of the history something like this works after the above code:
if(historyArray.count > kSearchHistorySize)
[historyArray removeLastObject];
The above only works if you add one string at a time, otherwise replace the 'if' with 'while' perhaps.