All submissions

Remove Duplicate Characters from a String

 

#include <stdio.h>
#include <stdint.h>

void remove_duplicates(char *str) {
	//Your logic here
    uint8_t seen[256] = {0};  // Track seen characters 
    uint8_t write_pos = 0;    // Position to write unique characters
    
    for (uint8_t read_pos = 0; str[read_pos] != '\0'; read_pos++) 
    {
        char current_char = str[read_pos];
        
        if (seen[(unsigned char)current_char] == 0)  // If character hasn't been seen before
        {
            seen[(unsigned char)current_char] = 1;  // Mark as seen
            str[write_pos] = current_char;          // Keep this character
            write_pos++;
        }
        // If character already seen, skip it (don't increment write_pos)
    }
    
    // Null-terminate the result
    str[write_pos] = '\0';
}

int main() {
    char str[101];
    fgets(str, sizeof(str), stdin);

    // Remove newline
    uint8_t i = 0;
    while (str[i]) {
        if (str[i] == '\n') {
            str[i] = '\0';
            break;
        }
        i++;
    }

    remove_duplicates(str);
    printf("%s", str);
    return 0;
}

 

 

 

 

Loading...

Input

programming

Expected Output

progamin