Given an array arr of size n , write a program to find the most frequent element in the array, i.e. the element which occurs the most number of times.

Problem Note

  • It is assumed that at least one element is repeated.
  • If two or more elements have the maximum frequency then return the element which is the smallest.

Example 1

Input: arr[] = [3, 9, 1, 3, 6, 3, 8, 1, 6]
Output: 3
Explanation: 3 appears three times in array which
is maximum frequency.

Example 2

Input: arr[] = [1, 15, 1, 9 , 2, 9, 10]
Output: 1
Explanation: 1 and 9 appears two times in array. Retrun 1 which is smallest.