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 Quick Sort Using Randomization
Java Program to Implement Word Wrap Problem
JPA/Hibernate Persistence Context
Java Program to Implement Iterative Deepening
Deploy a Spring Boot App to Azure
Hướng dẫn Java Design Pattern – Chain of Responsibility
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
Send email with authentication
Serialization và Deserialization trong java
Getting the Size of an Iterable in Java
Tạo số và chuỗi ngẫu nhiên trong Java
Getting Started with Stream Processing with Spring Cloud Data Flow
Java Program to subtract two large numbers using Linked Lists
Java Program to Check whether Directed Graph is Connected using DFS
Spring Boot - Flyway Database
Simultaneous Spring WebClient Calls
Simple Single Sign-On with Spring Security OAuth2
@Order in Spring
Vector trong Java
Java Program to Implement Sorted Singly Linked List
Abstract class và Interface trong Java
Spring’s RequestBody and ResponseBody Annotations
Java Program to Implement Interpolation Search Algorithm
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Java Program to Perform Partition of an Integer in All Possible Ways
Configure a Spring Boot Web Application
Spring Boot - Introduction
RestTemplate Post Request with JSON
How To Serialize and Deserialize Enums with Jackson
Java Program to Check Whether Topological Sorting can be Performed in a Graph
Java Program to Implement WeakHashMap API
JUnit5 Programmatic Extension Registration with @RegisterExtension