elementFrequency
to store the frequency of each element, and frequencyCount
to store the count of frequencies.maxFrequency
to keep track of the maximum frequency and result
to store the length of the longest valid prefix.elementFrequency
and frequencyCount
for each element.result
if the current prefix is valid.result
as the final answer.