APT SortedFreqs
Problem Statement
The frequency with which data occurs is sometimes
an important statistic. In this problem you'll determine
how frequently strings occur and return an array representing
the frequencies of each different/unique string. The array returned
contains as many frequencies as there are unique strings.
The returned frequencies represent an alphabetic/lexicographic ordering
of the unique words, so the first frequency is how many times the
alphabetically first word occurs and the last frequency is the
number of times the alphabetically last word occurs.
Consider these strings (quotes for clarity, they're not part of the strings).
{"apple", "pear", "cherry", "apple", "cherry", "pear", "apple", "banana"}
The array returned is {3,1,2,2} since the alphabetically
first word is "apple" which occurs 3 times; the second word
alphabetically is "banana" which occurs once, and the other
words each occur twice.
Definition
Class
public class SortedFreqs
{
public int[] freqs(String[] data)
{
// fill in code here
}
}
Notes
None
Constraints
- data will contain at most 50 elements
- each element of data will contain at most 50 characters,
all characters are lowercase.
Examples
-
data = {"apple", "pear", "cherry", "apple", "cherry", "pear", "apple", "banana"}
Returns: {3,1,2,2}
This is the example given above.
-
data = {"a","b","c",d"}
Returns {1,1,1,1}
-
data = {"a","a","a"}
Returns {3}
Owen L. Astrachan
Last modified: Thu Jan 13 11:45:58 EST 2005