Do you can show me a non-vector solution.
Hmm.. Thesnippet I posted dores not use vectors; instead it uses C-style null-terminated arrays of char (and dynamically allocated memory). With annotatations:
// input: an array of char terminated by a null-character
// returns: a dynamically allocated array of unique chars in the input
// terminated by a null-character
char* remove_duplicates_preserve_order( const char* cstr )
{
// 1. get a count of the number of unique chars in the input array
// ---------------------------------------------------------------
bool already_seen[UCHAR_MAX] = { false } ; // to look up already seen chars
int cnt_unique = 0 ; // number of unique chars in the string
// for every char in the input array till we encounter the null-character
for( const char* p = cstr ; *p ; ++p )
{
const unsigned char u = *p ;
// if we have not seen this char earlier, increment cnt_unique
// and set already_seen[u] to true
if( !already_seen[u] ) { already_seen[u] = true ; ++cnt_unique ; }
}
// 2. allocate memory for the result (a null-terminated array of chars)
// --------------------------------------------------------------------
char* result = new char[ cnt_unique + 1 ] ; // + 1 for the null-termination
result[cnt_unique] = 0 ; // null-terminate result
// 3. copy unique chars in the input array to the result array
// -----------------------------------------------------------
// look up table to check if we had alredy copied a char to the result
bool already_copied[UCHAR_MAX] = { false } ;
// for every …