Given an array of strings representing holiday greetings, write a function that determines whether the number of occurrences of each greeting is unique. In other words, no two different greetings should have the same frequency.
For example, given the array:
["Merry", "Cheerful", "Merry", "Happy", "Cheerful", "Joyful"]
The frequencies are:
Since both frequencies 2
and 1
occur more than once, the function should return false
.
Your task is to implement the function uniqueFrequencies
that returns true
if all greeting frequencies are unique, and false
otherwise.
Hint: Use a hashmap (or dictionary) to count occurrences, and then another data structure (like a set) to check for uniqueness of the frequency values.
Your function should have the following signature (shown here in a language-agnostic way):
function uniqueFrequencies(greetings: string[]) -> boolean
Input:
["Happy", "Joy", "Happy", "Cheerful"]
Counts:
Since the frequency 1
is not unique, the function should return false
.
Below is some starter code to help you begin your solution.