>> btw, the code I gave marcia was SLOW!!!!!! I really wanted to see what Naomi would do, I have the real code here which is much, much faster.
We saw quite a few solutions, some of which were pretty amazing, balancing conciseness with performance...
(Incidentally, this was once quite a common "how good are you" question for job interviews)
Using VFP Compiler I'd be inclined to inline some C++ especially for a larger string that you'd want to transform in-place rather than passing around, even by reference. Lots of different ways in C++ depending whether you are a vector or string fan, whether you want conciseness, least memory usage or performance. Te following is concise and shares the memory efficiency of those VFP solutions that truncate rather than concatenating strings:
#include <set>
#include <string.h>
typedef std::set<char> ChSet;
typedef std::set<char>::iterator ChSetIter;
/* Remove all the duplicate chars from the given string in-place */
/* void dedupe_string(char *str, int sz)
{
ChSet cset;
ChSetIter csiter;
int lastpos = -1;
for (int i = 0; i < sz; i++){
char ch = str[i];
csiter = cset.find(ch);
if (csiter == cset.end()) { // Unique char
cset.insert(ch);
if (-1 == lastpos) {
continue; // means we do not have previous holes
}
else {
str[lastpos] = ch; // move our unique char to fill the gap
str[i] = '\0'; // create a hole in place of the dup char
lastpos++;
}
}
else { // Found duplicate char
printf("%c ", ch);
if (-1 == lastpos)
lastpos = i;
else {
str[i] = '\0';
}
}
}
/* printf("\n");
}
"... They ne'er cared for us
yet: suffer us to famish, and their store-houses
crammed with grain; make edicts for usury, to
support usurers; repeal daily any wholesome act
established against the rich, and provide more
piercing statutes daily, to chain up and restrain
the poor. If the wars eat us not up, they will; and
there's all the love they bear us."
-- Shakespeare: Coriolanus, Act 1, scene 1