#C14818. Frequency Counter and Mode Finder
Frequency Counter and Mode Finder
Frequency Counter and Mode Finder
You are given a list of n integers. Your task is to compute two things:
- A frequency dictionary that counts how many times each unique integer appears. The dictionary should be output in sorted order (increasing numerical order of the keys).
- The integer that appears most frequently. In the event of a tie, choose the smallest integer among those.
Mathematically, let the frequency of an integer x be denoted by \(f(x)\). Then the mode is defined as the integer m such that $$ \begin{aligned} m = \min\{x \mid f(x) = \max_{y}f(y)\} \end{aligned} $$
Your program should read input from stdin
and output the results to stdout
as described in the Input/Output sections.
inputFormat
The first line contains an integer n, representing the number of integers. The second line contains n space-separated integers.
Example:
4 1 2 3 4
outputFormat
The output consists of two lines:
- The first line displays the frequency dictionary. For each unique integer, output the key and its count in the format
key:count
separated by a single space. The keys must be in increasing order. - The second line displays the most frequent integer (if there is a tie, the smallest among them).
Example:
1:1 2:1 3:1 4:1 1## sample
1
1
1:1
1
</p>