94. Remove Duplicate Characters from a String

Back To All Submissions
Previous Submission
Next Submission

Code

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

void remove_duplicates(char *str) {
	int alpha[95] = {0};
    int len = 0;

    while(str[len++] != '\0');

    char temp[len];
    int index = 0, tIndx = 0;
    for(; index < len; index++) {
        if( (alpha[str[index] - 0x20]) == 0 ){
            temp[tIndx++] = str[index];
        }
        alpha[str[index] - 0x20] = 1;
    }
    temp[tIndx++] = '\0';
    for(int i = 0; i < len; i++) {
        str[i] = temp[i];
        if(temp[i] == '\0') {
            break;
        }
    }

}

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

 

 

 

Was this helpful?
Upvote
Downvote