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:
Java Program to Implement Sieve Of Sundaram
Java Program to Implement Hash Tables
Java Program to Construct an Expression Tree for an Postfix Expression
Spring Cloud Connectors and Heroku
Java Program to Compute Cross Product of Two Vectors
Java Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Spring Boot - Apache Kafka
Introduction to Using Thymeleaf in Spring
Java – Create a File
Simplify the DAO with Spring and Java Generics
Java Program to Implement Direct Addressing Tables
Using a Mutex Object in Java
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
JUnit 5 @Test Annotation
Lập trình hướng đối tượng (OOPs) trong java
Implementing a Binary Tree in Java
Thao tác với tập tin và thư mục trong Java
REST Web service: Basic Authentication trong Jersey 2.x
Introduction to the Java NIO Selector
Comparing Objects in Java
Spring Security OAuth2 – Simple Token Revocation
Overview of Spring Boot Dev Tools
Registration – Activate a New Account by Email
Java Program to Implement the One Time Pad Algorithm
Cơ chế Upcasting và Downcasting trong java
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Spring Cloud Bus
Request a Delivery / Read Receipt in Javamail
The Basics of Java Security
Java Program to Implement Word Wrap Problem