A logger records n integer readings, which may repeat. Starting from an empty binary search tree, insert the readings in the order given, using this rule: to insert a value x, walk down from the root, going left if x is strictly less than the current node's value, and right if x is greater than or equal to the current node's value, until an empty spot is reached, where a new node is created. Every reading becomes its own node — the tree may therefore contain several nodes with the same value. No rebalancing is performed.
Report the mode: the value that occurs most often among the n readings. If several values are tied for the highest frequency, report the smallest of them.
Line 1: an integer n.
Line 2: n space-separated integers — the readings, in insertion order (not necessarily distinct).
A single integer: the mode of the readings.
Example 1
Input
7 5 3 5 8 3 5 1
Expected
5
Explanation
Value 5 appears 3 times, more than any other value (3 appears twice, 8 and 1 once each), so the mode is 5.
Example 2
Input
6 2 2 4 4 1 1
Expected
1
Explanation
Values 2, 4, and 1 each appear exactly twice, a three-way tie. The smallest of the tied values is 1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →