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:
Introduction to Using Thymeleaf in Spring
Java Program to Implement Aho-Corasick Algorithm for String Matching
Hướng dẫn Java Design Pattern – Bridge
Extract links from an HTML page
Introduction to Thread Pools in Java
Java Program to Solve a Matching Problem for a Given Specific Case
Test a REST API with Java
Java Program to Represent Graph Using Incidence Matrix
The Difference Between Collection.stream().forEach() and Collection.forEach()
Spring Boot - Google Cloud Platform
Java Program to Implement Queue using Two Stacks
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Java Program to Implement Warshall Algorithm
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Reading an HTTP Response Body as a String in Java
Java Program to Implement Binary Search Tree
Java 8 Collectors toMap
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Split a String in Java
Updating your Password
Serve Static Resources with Spring
Show Hibernate/JPA SQL Statements from Spring Boot
The StackOverflowError in Java
Java Program to Implement Sorted Circularly Singly Linked List
Java Program to Implement Jarvis Algorithm
Comparing Arrays in Java
Java Program to Implement Iterative Deepening
Chương trình Java đầu tiên
Java Program to Implement Sorted Doubly Linked List
Java Program to Implement Doubly Linked List
Prevent Brute Force Authentication Attempts with Spring Security
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java