All submissions

Remove Duplicate Characters from a String

Easy and Simple Code to Understand

Code

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

void remove_duplicates(char *str) {
	int index = 0; //To traverse through the character array
    int map_index = 0; //index to traverse through the map array to store the unique elements in order wise
    int map[256] = {0}; //Map to store the unique elements order wise

    while(str[index]){
        if(!map[str[index]]){  //If the character is found for the first time
            str[map_index++] = str[index];
            map[str[index]] = -1; //To avoid further inclusions we add this assignment
        }
        index++;
    }
    str[map_index]='\0';//To terminate the string
}
    

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;
}

Solving Approach

 

 

 

Loading...

Input

programming

Expected Output

progamin