Counting Unique Characters in a String

Counting unique characters in a String given by the user

It is extremely easy :)

public static int countUniqueCharacters(String input) {
boolean[] isItThere = new boolean[Character.MAX_VALUE];
for (int i = 0; i < input.length(); i++) {
isItThere[input.charAt(i)] = true;
}

int count = 0;
for (int i = 0; i < isItThere.length; i++) {
if (isItThere[i] == true){
count++;
}
}

return count;
}

Example for input "aab"

First for-cycle goes 3 times, each time for one char.

Value of "a" is 97, so it turns isItThere[97] to true, then second "a" is involved, which is doing the same, isItThere[97] is set to true again (hence changing nothing).

After that "b" is involved, value of char "b" is 98, therefore isItThere[98] is set to true.

And then you have second for-cycle, where you cycle through the all isItThere array. If you find any true statement, you increment count. In our case, you find isItThere[97] and isItThere[98] as true statement, it means you increment twice and returning 2.

Counting unique characters in a string

You can do System.out.println(countUniqueCharacters(s)); in the main method, to output the return value of your method. After a return, you cannot add more code. I did it for you and the output is 12, so it seems to be that there is also something wrong with your algorithm.

    int uniqeCharsCount = countUniqueCharacters(s);
System.out.println("The number of uniqe chars is " + uniqeCharsCount);

Output: 12

Your algorithm:

Actually you are checking every char, if this char is one more time in the string before. But you should also check if the char is anywhere in the string after the current index. You can fix it if you change your if condition to if (i != lowerCase.indexOf(characters[i]) || i != lowerCase.lastIndexOf(characters[i]))

Output of the fixed version: 3 (n, h, r)

Counting unique characters in string given by user C programming

  • else if( sent[i]!='0') ...

  • Initialize numOnes numZeros.

  • for loop will be for(i=0;sent[i]!='\0';i++)

Also you can simplify the print logic. Calculate the length of the input.

if(numZeroes+numOnes < len )
// Goodness is zero
else
// Goodness is numOnes

Implementation:

int main()
{
int numZeros=0, numOnes=0;
char sent[50];
printf("Enter a string with no spaces: ");
scanf(" %s", sent);

for(int i=0; sent[i]; ++i)
(sent[i]=='0')?numZeros++:numOnes++;

printf("Goodness of the input is %d", (numOnes+numZeros<strlen(sent))?0:numOnes);
return 0;
}

Trying to extract/count the unique characters in a string (of class character)

In base R you can do:

df$char_count <- sapply(strsplit(df$Text, ""), function(x) length(unique(x)))

df
#> Text char_count
#> 1 banana 3
#> 2 banana12 5
#> 3 Ace@343 6

Data

df <- data.frame(Text = c("banana", "banana12", "Ace@343"))

Created on 2021-11-12 by the reprex package (v2.0.0)

Python: Dict for counting unique letters in string

You can use a dict comprehension

mydict_countalpha = {alpha[x]:sent.count(alpha[x]) for x in range(len(alpha))}

But there is no need to keep looking up the index. Loop over alpha directly

mydict_countalpha = {ch:sent.count(ch) for ch in alpha}

The way I would normally do this, however, is using collections.Counter

from collections import Counter
mydict_countalpha = {k: v for k, v in Counter(sent).items() if k in alpha}

edit: added for loop version

mydict_countalpha = {}
for ch in alpha:
mydict_countalpha[ch] = sent.count(ch)

Counting the occurrence of unique letters in a string in Python 3

if you need to count characters in string, try the following

a = "aaabbcccd"
b = dict.fromkeys(a, 0)
for i in a:
b[i] += 1

b now holds the counts you require:

{'a': 3, 'c': 3, 'b': 2, 'd': 1}


Related Topics



Leave a reply



Submit