Get the Most Frequent Element in an Array in Java


In Java, Array is a non-primitive data type which stores values of similar data type.

As per the problem statement we have to detect which element is repeated maximum times in an array for maximum number of times and print its frequency.

An array can contain duplicate values as well. So, the number of times an element is present in an array, that is called frequency of the element in the array. And the element whose frequency is more that is the most frequent element in the array.

Let’s see how we can do it by using the Java programming language.

To Show You Some Instances

Instance-1

Suppose the original array is {1, 2, 3, 1, 5, 7, 5, 5, 9}.

After performing the array operation to find the array element which is occurring maximum time the result will be −

Most frequent element is − 5

Instance-2

Suppose the original array is {7, 2, 3, 2, 5, 7, 3, 5, 9}.

After performing the array operation to find the array element which is occurring maximum time the result will be −

Most frequent element is − 7

Instance-3

Suppose the original array is {77, 22, 45, 22, 32, 77, 45, 77, 77}

After performing the array operation to find the array element which is occurring at maximum time the result will be −

The most frequent element is − 77

Algorithm

  • Step 1 − Declare and initialize an integer array.

  • Step 2 − Take an int variable max_count and initialize it with 0. And declare another int variable count to keep track of the number of times an element is present in the array.

  • Step 3 − Check for condition count > max_count. Then assign count value to max_count.

  • Step 4 − Finally, print the element which has max_count value.

Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length

Below refers to the syntax of it −

array.length

Where, ‘array’ refers to the array reference.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Initialization of Array Elements

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Initialization of Array Elements

Example

In this approach, array elements will be initialized in the program. Then as per the algorithm get the most frequent element in an array.

public class Main{

   //main method
   public static void main(String[] args){
   
      //Declare and initialize the array elements
       int[] arr = { 1, 2, 3, 1, 5, 7, 5, 5, 9 };
       
      //get the length of the array
      int n = arr.length;
      int max_count = 0;
      int maxfreq = 0;
       
      //Logic implementation
      for (int i = 0; i < n; i++){
         int count = 0;
         for (int j = 0; j < n; j++){
            if (arr[i] == arr[j]){
               count++;
            }
         }
        
         if (count > max_count){
            max_count = count;
            maxfreq = arr[i];
         }
      }
      //print the result
      System.out.print("Most frequent element is: " + maxfreq);
   }
}

Output

Most frequent element is: 5

Approach-2: By Using User Defined Method

Example

In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside the method as per the algorithm get the most frequent element in an array.

public class Main{

   //main method
   public static void main(String[] args){
   
      //Declare and initialize the array elements
      int[] arr = { 1, 2, 3, 1, 5, 7, 5, 5, 9 };
      
      //get the length of the array
      int n = arr.length;
      
      //call the user defined method and print the result
      System.out.print("Most frequent element is: " + freq(arr, n));
   }
    
   //user defined method
   public static int freq(int[] arr, int n){
      int max_count = 0;
      int maxfreq = 0;
       
      //Logic implementation
      for (int i = 0; i < n; i++) {
         int count = 0;
         for (int j = 0; j < n; j++) {
            if (arr[i] == arr[j]) {
               count++;
            }
         }
       
         if (count > max_count) {
            max_count = count;
            maxfreq = arr[i];
         }
      }
       return maxfreq;
   }
}

Output

Most frequent element is: 5

In this article, we explored how to find the most frequent element in an array in Java.

Updated on: 05-Jan-2023

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements