AlgoLounge

Most Frequent Element

easy

Find the most frequent element in an array. If multiple elements have the same maximum frequency, return the smallest element.

Examples

Example 1

Input:  [3, 3, 3, 2, 1]
Output: 3

3 appears most frequently (3 times)

Example 2

Input:  [3, 1, 3, 1, 2]
Output: 1

Both 3 and 1 appear twice, return smaller element (1)

Constraints

  • Array contains at least 1 element
  • Handle ties by returning smallest element
TypeScript
Loading editor…
List of test cases (10)
Case 1
arr = [3,3,3,2,1]
Case 2
arr = [3,1,3,1,2]
Case 3
arr = [5]
Case 4
arr = [1,2,3,4,5]
Case 5
arr = [10,10,5,5,3]
Case 6
arr = [7,7,7,7]
Case 7
arr = [4,4,4,2,2,2,1]
Case 8
arr = [100,200,100,200,100]
Case 9
arr = [1,1,2,2,3,3,4,4]
Case 10
arr = [9,8,7,6,5,4,3,2,1]