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:
Converting Iterator to List
Java Program to Implement Repeated Squaring Algorithm
Java Program to Find Number of Articulation points in a Graph
Java Program to Implement Ternary Search Tree
Ignore Null Fields with Jackson
Java Program to Construct an Expression Tree for an Postfix Expression
Java Program to Find Nearest Neighbor for Static Data Set
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Daemon Threads in Java
Creating a Web Application with Spring 5
Một số nguyên tắc, định luật trong lập trình
The Basics of Java Security
Updating your Password
Java Program to Find kth Largest Element in a Sequence
Java Program to Perform Partition of an Integer in All Possible Ways
Consumer trong Java 8
Mix plain text and HTML content in a mail
Java Program to Perform the Shaker Sort
@Lookup Annotation in Spring
Java NIO2 Path API
New in Spring Security OAuth2 – Verify Claims
Java Program to Implement Bloom Filter
How to Get All Spring-Managed Beans?
Serve Static Resources with Spring
Java Program to Create a Balanced Binary Tree of the Incoming Data
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Java Program to Implement Wagner and Fisher Algorithm for online String Matching
Spring Boot Integration Testing with Embedded MongoDB
Configuring a DataSource Programmatically in Spring Boot
Hướng dẫn sử dụng Java Reflection
Java Program to Implement Meldable Heap
Java Program to Check if it is a Sparse Matrix