Remove Duplicate Characters from a String

Code

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

void remove_duplicates(char *str) {
	//Your logic here
    int i=1,j=0,a=1;
    char res[101];
    res[j] =str[0];
    j++;
    while(str[i] != '\0'){
       for(int k=0;k<j;k++){
        if (str[i]==res[k]) {
            a=0;
            break;
            }
       }
      if(a==1){
         res[j]=str[i];
         j++;
         }
      i++;
      a=1;

    }
    res[j] = '\0';
    i=0;
    str[0]='\0';
    while(res[i]!='\0'){
        str[i]=res[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

 

 

 

Upvote
Downvote
Loading...

Input

programming

Expected Output

progamin