Learn
Exam

Finding the Most Frequent Element and Its Count in an Array

Difficulty
STARTER

Problem

Given an array numbers consisting of natural numbers, write a function solution that returns the most frequent element in numbers and its count as an array.


Constraints

  • Each element in numbers is an integer between 0 and 500.
  • numbers is not empty, and its length does not exceed 100.
  • The result should be returned as a list in the form [element, count].
  • There are no cases where multiple elements have the same maximum count.

Test Case

Test Case Number numbers return
1 [2, 2, 4, 5] [2, 2]
2 [10, 5, 100, 5, 3, 5, 0] [5, 3]
3 [1] [1, 1]

Explanation for Test Case 1

  • The most frequent element in numbers is 2, appearing 2 times.

Explanation for Test Case 2

  • The most frequent element in numbers is 5, appearing 3 times.

Explanation for Test Case 3

  • The most frequent element in numbers is 1, appearing 1 time.