This is a java program to find the mode of a set. The mode of a set is defined as the highest occurring element in the set. We count the occurrence of each of the element and print the element whose count is highest.
Here is the source code of the Java Program to Find the Mode in a Data Set. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
//This is a java program to find the mode for a given sequence of numbers
import java.util.Random;
public class Mode
{
static int N = 20;
static int[] sequence = new int[N];
public static int mode()
{
int maxValue = 0, maxCount = 0;
for (int i = 0; i < sequence.length; ++i)
{
int count = 0;
for (int j = 0; j < sequence.length; ++j)
{
if (sequence[j] == sequence[i])
++count;
}
if (count > maxCount)
{
maxCount = count;
maxValue = sequence[i];
}
}
return maxValue;
}
public static void main(String args[])
{
Random random = new Random();
for (int i = 0; i < N; i++)
sequence[i] = Math.abs(random.nextInt(100));
System.out.println("The set of numbers are: ");
for (int i = 0; i < N; i++)
System.out.print(sequence[i] + " ");
System.out.println("\nThe mode of the set is: " + mode());
}
}
Output:
$ javac Mode.java $ java Mode The set of numbers are: 85 3 80 56 37 47 13 11 94 38 6 12 10 31 52 67 81 98 43 37 The mode of the set is: 37
Related posts:
Multipart Upload with HttpClient 4
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
A Guide to Spring Cloud Netflix – Hystrix
Bootstrap a Web Application with Spring 5
Spring Boot - Securing Web Applications
So sánh HashMap và Hashtable trong Java
Spring Boot - Bootstrapping
Compare Two JSON Objects with Jackson
Lớp Arrarys trong Java (Arrays Utility Class)
Auditing with JPA, Hibernate, and Spring Data JPA
Java 8 Stream findFirst() vs. findAny()
Java Program to Implement Hash Tree
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
Java Program to Implement Gabow Algorithm
Jackson Date
Java Program to Find Inverse of a Matrix
The Order of Tests in JUnit
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Hướng dẫn Java Design Pattern – Prototype
Java Program to Implement Hopcroft Algorithm
Simple Single Sign-On with Spring Security OAuth2
Java Program to Generate Random Numbers Using Probability Distribution Function
Hướng dẫn Java Design Pattern – Chain of Responsibility
Java Program to Solve any Linear Equations
Java Program to Generate Date Between Given Range
Guide to Apache Commons CircularFifoQueue
Introduction to Apache Commons Text
Giới thiệu luồng vào ra (I/O) trong Java
Spring Security Login Page with React
Hướng dẫn Java Design Pattern – Dependency Injection
Working with Kotlin and JPA
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize