- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to calculate mode in Java
In statistics math, a mode is a value that occurs the highest numbers of time. For Example, assume a set of values 3, 5, 2, 7, 3. The mode of this value set is 3 as it appears more than any other number.
Algorithm
1.Take an integer set A of n values. 2.Count the occurrence of each integer value in A. 3.Display the value with the highest occurrence.
Example
public class Mode { static int mode(int a[],int n) { int maxValue = 0, maxCount = 0, i, j; for (i = 0; i < n; ++i) { int count = 0; for (j = 0; j < n; ++j) { if (a[j] == a[i]) ++count; } if (count > maxCount) { maxCount = count; maxValue = a[i]; } } return maxValue; } public static void main(String args[]){ int n = 5; int a[] = {0,6,7,2,7}; System.out.println("Mode ::"+mode(a,n)); } }
Output
Mode ::7
Advertisements