What is the quickest way to find the first character which only appears once in a string?
Different approach here. scan each element in the string and create a count array which stores the repetition count of each element. Next time again start from first element in the array and print the first occurrence of element with count = 1
C code
-----
#include
#include
int main(int argc, char *argv[])
{
char t_c;
char *t_p = argv[1] ;
char count[128]={'\0'};
char ch;
for(t_c = *(argv[1]); t_c != '\0'; t_c = *(++t_p))
count[t_c]++;
t_p = argv[1];
for(t_c = *t_p; t_c != '\0'; t_c = *(++t_p))
{
if(count[t_c] == 1)
{
printf("Element is %c\n",t_c);
break;
}
}
return 0;
}