Cranes at VIIT Q6

Find the Most Frequent Element in an Array

Write a C program that finds the most frequent element in an array.

Problem Statement:

Input Format:

Output Format:

Example Inputs & Outputs:

Enter the number of elements: 7
Enter the elements: 1 3 2 3 4 1 3
Most frequent element: 3
            
Enter the number of elements: 5
Enter the elements: 5 1 5 1 2
Most frequent element: 1
            

Constraints:

Hint:

To solve this problem, follow these steps:

  1. Store Frequencies:
    • Create an array or a map to store the frequency of each element.
    • Traverse the input array and increment the frequency of each element.
  2. Find Maximum Frequency:
    • Traverse the frequency array/map to find the element with the highest frequency.
  3. Check for Multiple Maximums:
    • If multiple elements have the same highest frequency, choose the smallest element.
  4. Print the Result:
    • Print the element with the highest frequency or the smallest among elements with equal frequency.

Formula Breakdown:

Task: Implement the above logic and write a C program to find the most frequent element in an array.