Find most frequent element in an array – Java Program to Find the Most Frequent Element in the Array

Find most frequent element in an array: In the previous article, we have seen Java Program to Find the Frequency of Each Element of an Array

In this article we are going to see how to find out the most occurring element in an array in Java.

Java Program to Find the Most Frequent Element in the Array

Most frequent element in an array: Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to find out the most occurring element in an array.

Method-1: Java Program to Find the Most Frequent Element in the Array By Static Initialization of Array Elements and User Defined Function

Approach:

  • Create an array with elements, and another blank array of same size called freq.
  • Set all the elements in the blank array to -1 using fill( ) library function.
  • Display the array elements to the user.
  • Pass both the arrays into an user function frequency ( ) that finds and stores the number of occurrence of elements.
  • Use a counter variable to count the number of times the element occurs inside the array.
  • Store it in the freq array at same location as the element.
  • Use a for loop to compare and find out the index of the largest number in the freq[ ] Then print the array element at that index from the main array and its occurrence too.

Program:

import java.util.*;
import java.util.Scanner;

public class Main
{
    public static void main(String args[])
    {
        // Creating the array
        int arr[] = {12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68,10,20,30,20,30,50,10,50,20};
        int freq[] = new int[arr.length];
        // Sets all elements in the array to -1
        Arrays.fill(freq, -1);
        // Prints the array elements
        System.out.println("The array elements are "+Arrays.toString(arr));
        
        unique(arr,freq);

    }
    
    // Function that couunts the frequency of elements
    static void unique(int arr[], int freq[])
    {
        int count;

        for(int i = 0; i<arr.length; i++)
        {
            // Resets count to 1 after each element
            count=1;
            for(int j = i + 1; j<arr.length;j++)
            {
                // If another occurence of the current element is found 
                // in the array then increments the counter
                if(arr[i]==arr[j])
                {
                    count++;
                    freq[j] = 0;
                }
            }
            // Stores the frequency of each element
            if(freq[i]!=0)
            {
                freq[i] = count;
            }
        }

        // Prints the most occuring element
        int highestFreq=freq[0], mostOccuringIndex=0;
        for(int i=0;i<freq.length;i++)
        {
            if(highestFreq<freq[i])
            {
                highestFreq=freq[i];
                mostOccuringIndex = i;
            }
        }

        // Prints the most occuring element and its occurence
        System.out.println("The most occuring element is "+arr[mostOccuringIndex]+
        " which has occured "+freq[mostOccuringIndex]+" times.");
    }
}

Output:

The array elements are [12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68, 10, 20, 30, 20, 30, 50, 10, 50, 20]
The most occurring element is 20 which has occurred 3 times.

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: