94. Remove Duplicate Characters from a String

Back To All Submissions
Previous Submission
Next Submission

Code

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

int find_len(char* str){
    int count = 0;
    while(*str++ != '\0'){
        count++;
    }
    return count;
}

void remove_duplicates(char *str) {
    int n = find_len(str);
    char found[n + 1];
    int found_index = 0;

    for(int i = 0; i < n; i++){
        int founded = 0;

        for(int j = 0; j < found_index; j++){
            if(str[i] == found[j]){
                founded = 1;
                break;
            }
        }

        if(!founded){
            found[found_index++] = str[i];
        }
    }
    found[found_index++] = '\0';
    printf("%s", found);

}

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);
    
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote