#K37222. Most Frequent Element
Most Frequent Element
Most Frequent Element
Given an array of integers of length N, your task is to find the most frequent element in the array. In the event of multiple elements having the same highest frequency, you must return the smallest element among them.
This problem can be mathematically described as follows: Given an integer array \( A = [a_1, a_2, \dots, a_N] \), find an element \( x \) such that \( \text{freq}(x) = \max_{y \in A}\text{freq}(y) \) and for all \( z \) with \( \text{freq}(z) = \text{freq}(x) \), we have \( x \le z \).
Input will be read from standard input and output should be written to standard output.
inputFormat
The first line contains a single integer N, representing the number of elements in the array. The second line contains N space-separated integers.
outputFormat
Output a single integer — the most frequent element as described. In the case of a tie, output the smallest element among those with the highest frequency.## sample
5
1 2 2 1 3
1