
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- Most frequent element in an array in C++
- Find the second most frequent element in array JavaScript
- Finding the most frequent word(s) in an array using JavaScript
- Find the least frequent element in an array using Python
- JavaScript Program for the Least Frequent Element in an Array
- Find most frequent element in a list in Python
- Program to find out the index of the most frequent element in a concealed array in Python
- Find Second most frequent character in array - JavaScript
- C# program to find the most frequent element
- Write a program in C++ to find the most frequent element in a given array of integers
- Program to find frequency of the most frequent element in Python
- Most Frequent Subtree Sum in C++
- Most Frequent Number in Intervals in C++
- Write a program in C++ to find the top K frequent element in an array of integers
- Finding the second most frequent character in JavaScript
