beyondgrader.com Logo
DemoBrowseAboutTeamLogin

Count Letters with Map

Geoffrey Challen // 2020.10.0

Create a method countLetters. countLetters accepts an array of Strings and returns a Map from Strings to Ints.

The map should contain counts of the passed Strings based on their first letter. For example, provided the array {"test", "me", "testing"} your Map should be {"t": 2, "m": 1}. You should ignore empty Strings and not include any zero counts.

As a reminder, you can retrieve the first character of a String as a char using index notation: example[0]. You may find substring more helpful. You may also want to utilize the Elvis operator ?: for retrieving a default value from a map when the requested key does not exist.