Lio
Lio

Reputation: 19

Count the occurrence of each character in a string and put it in an array

So I'm counting the occurrences of each letter in a string from a Java file I scanned earlier on. I've removed all unnecessary characters from the string and the string now contains only unicode letters. An example:

String letters = "wecanendupclimbingthewrongladderandpursuesomeoneelsesversionofsuccess";

So let's say I create an array to contain the frequency of each letter.

int[] frequency = new int[26];

And I'm putting the occurrences in order according to the letters in the alphabet. So let's say a is 5, and b is 3 and c is 10. It should be something like this:

frequency = {5, 3, 10};

How best do you think I can do this?

Upvotes: 1

Views: 2499

Answers (3)

Majed Badawi
Majed Badawi

Reputation: 28404

The good solution would be using a TreeMap in order to keep the characters sorted alphabetically and then get the corresponding list of values (number of occurrences) after looping over the characters.

Here is an example:

String str = "wecanendupclimbingthewrongladderandpursuesomeoneelsesversionofsuccess";
Map<Character,Integer> map = new TreeMap<>();
for(char c : str.toCharArray())
    map.put(c, map.getOrDefault(c, 0)+1);
System.out.println(map.values());

Output:

[3, 1, 4, 4, 11, 1, 2, 1, 3, 3, 2, 7, 5, 2, 4, 8, 1, 4, 1, 2]

If you want to include absent characters (those with zero occurrences in the string), you can initialize the map with all the alphabets at first as follows:

for(int i = 0; i < 26; i++)
    map.put((char)(97 + i), 0);

Then, the output would be:

[3, 1, 4, 4, 11, 1, 2, 1, 3, 0, 0, 3, 2, 7, 5, 2, 0, 4, 8, 1, 4, 1, 2, 0, 0, 0]

Upvotes: 1

0xh3xa
0xh3xa

Reputation: 4859

Calculate the index of character related to the frequency array by (ch - 'a')

Subtract character 'a' from the character

        String letters = "aaaabbabbz";
        letters = letters.toLowerCase();
        int[] frequency = new int[26];
        for (char ch : letters.toCharArray()) {
            int index = ch - 'a';
            frequency[index]++;
        }
        System.out.println(Arrays.toString(frequency));

        // in case you want only the characters that appear you can use IntStream and filter
        int[] result = IntStream.of(frequency).filter(i -> i > 0).toArray();
        System.out.println(Arrays.toString(result));

, output

[5, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
[5, 4, 1]

Upvotes: 2

ARC
ARC

Reputation: 66

Plenty of ways to skin this cat, I think the easiest thing might be sort based off of the character ASCII value. So for example 'a' is valued at 97, so whatever letter you encounter just cast it and subtract 97 to fit it into the proper place in the array (assuming you want arr[0] = frequency of 'a', arr[1] = frequency of 'b', etc). So if you occurred letter 'letter', just increment the array value at index int(letter) - 97, if that makes sense. Again, plenty of ways to solve this problem and your thought process is good.

Or as mentioned in the comments you could just use a key/value map lol

Upvotes: 0

Related Questions