Remove duplicate characters from a string (keep first occurrences).
#include <stdbool.h>
void remove_dup_chars(char *s)
{
bool seen[256] = { false };
char *w = s;
for (char *r = s; *r; r++) {
unsigned char c = (unsigned char)*r;
if (!seen[c]) { seen[c] = true; *w++ = *r; }
}
*w = '\0';
}
/* "programming" -> "progamin" */
Points: a seen-table plus read/write pointers: O(n) time, O(1) space; keeps the order of first occurrences.