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 Multi-line String
Java Program to Implement Segment Tree
Hướng dẫn sử dụng luồng vào ra ký tự trong Java
Spring Boot - Google Cloud Platform
Java Program to Implement Lloyd’s Algorithm
Model, ModelMap, and ModelAndView in Spring MVC
Spring AMQP in Reactive Applications
Hashing a Password in Java
Java Program to Implement VList
Java Program to Find Transitive Closure of a Graph
HttpClient 4 – Follow Redirects for POST
Java Program to Implement Leftist Heap
SOAP Web service: Authentication trong JAX-WS
@Order in Spring
Serialization và Deserialization trong java
Java – Reader to InputStream
Hướng dẫn Java Design Pattern – Abstract Factory
Java Timer
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Java Program to Find Maximum Element in an Array using Binary Search
Introduction to Spring Security Expressions
Java Program to Solve any Linear Equation in One Variable
Enum trong java
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Explain about URL and HTTPS protocol
Composition, Aggregation, and Association in Java
Java Program to Implement Sieve Of Sundaram
Java Program to Find the GCD and LCM of two Numbers
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Java Program to Implement Treap
LIKE Queries in Spring JPA Repositories
Hướng dẫn Java Design Pattern – Bridge