Highest occurring element in an array

Beginner Problems Basic Hashing Easy
  • The underlying concept of this problem is extensively used in data analytics, specifically in areas requiring analysis of user behaviour
  • For example, music streaming apps often need to find the most frequently listened song among a pool of users
  • In this case, the 'array of n integers' could represent users' song preferences and the 'most frequent element' would be the most popular song
  • Furthermore, finding the 'smallest of them' could be analogous to identifying the most popular song with the shortest duration, which could influence decisions in areas like marketing or content curation

Given an array of n integers, find the most frequent element in it i.e., the element that occurs the maximum number of times. If there are multiple elements that appear a maximum number of times, find the smallest of them.


Please note that this section might seem a bit difficult without prior knowledge on what hashing is, we will soon try to add basics concepts for your ease! If you know the concepts already please go ahead to give a shot to the problem. Cheers!

Examples:

Input: arr = [1, 2, 2, 3, 3, 3]


Output: 3


Explanation: The number 3 appears the most (3 times). It is the most frequent element.

Input: arr = [4, 4, 5, 5, 6]


Output: 4


Explanation: Both 4 and 5 appear twice, but 4 is smaller. So, 4 is the most frequent element.

Input: arr = [10, 9 ,7]

Constraints

  • 1 <= n <= 105
  • 1 <= arr[i] <= 104

Company Tags

TCS Cognizant Accenture Infosys Capgemini Wipro IBM HCL Tech Mahindra MindTree