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 len=0, count=0, flag, i;
    char temp[101];
    while(*(str + len) != '\0') {
        len++;
    }

    for(i=0; i<len; i++) {
        flag=0;
        for(int j=0; j<count; j++) {
            if(*(str + i)==temp[j]) {
                flag=1;
                break;
            }

        }

        if(flag == 0) {
            temp[count]=*(str + i);
            count++;
        }
        
    }

    temp[count]='\0';
    i=0;
    while(temp[i]!='\0') {
        *(str + i) = temp[i];
        i++;
    }
    *(str+i)='\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;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote