Most Frequent Element
EasyPractice library · +10 XPhash mapcountingfrequency
Given a list of N integers, find the integer that appears most frequently. If there is a tie (multiple integers appear with the same maximum frequency), output the smallest of these integers.
Input
The first line contains an integer N (1 <= N <= 1000). The second line contains N space-separated integers, each between -1000 and 1000.
Output
Print a single integer, the most frequent element according to the rules.
Constraints
1 <= N <= 1000 -1000 <= element <= 1000
Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests
Example 1
Input
5 1 2 2 3 3
Output
2
Numbers 2 and 3 both appear twice. The smallest is 2.
Example 2
Input
7 5 5 5 1 1 2 2
Output
5
Number 5 appears 3 times, which is more than 1 or 2. So 5 is the answer.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 1 2 2 3 3
Example 2 input
7 5 5 5 1 1 2 2