ccountcharactercharactercount

How can I find the number of unique characters in a string?


I have found nothing particular for this purpose.

I am trying to figure out a function that counts each of the characters' occurrences in a string, so that I can pull them out at the end from the length to find how many homogeneous characters are used in that string.

I've tried with nested loop, the first to apply and the second to scan the string and conditionally fulfill the character if it does not appear elsewhere in the string:

size_t CountUniqueCharacters(char *str)
{
    int i,j;
    char unique[CHAR_MAX];
    for(i=strlen(str); i>=0; i--)
    {
        for(j=strlen(str); j>=0; j--)
        {
            if(str[i] != unique[j])
                unique[j] = str[i];
        }
    }
    return strlen(unique);
}

This didn't work well.

This is useful if you are willing to limit someone to type lazy names such as "aaaaaaaaaaaaa".


Solution

  • This method has O(n^2) complexity, but it's very possible (though a bit more complex) to do this in O(n).

    int CountUniqueCharacters(char* str){
        int count = 0;
    
        for (int i = 0; i < strlen(str); i++){
             bool appears = false;
             for (int j = 0; j < i; j++){
                  if (str[j] == str[i]){
                      appears = true;
                      break;
                  }
             }
    
             if (!appears){
                 count++;
             }
        }
    
        return count;
    }
    

    The method iterates over all the characters in the string - for each character, it checks if the character appeared in any of the previous characters. If it didn't, then the character is unique, and the count is incremented.