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:
Consumer trong Java 8
How to Read a Large File Efficiently with Java
Spring Boot - Web Socket
Check if a String is a Palindrome in Java
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Extra Login Fields with Spring Security
Guide to the Synchronized Keyword in Java
Java Program to Implement Fibonacci Heap
Spring Boot - Tracing Micro Service Logs
Java Program to Implement Double Order Traversal of a Binary Tree
Java Program to implement Sparse Vector
Spring MVC and the @ModelAttribute Annotation
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Spring’s RequestBody and ResponseBody Annotations
An Introduction to Java.util.Hashtable Class
Java Program to Find All Pairs Shortest Path
Java Program to Implement Extended Euclid Algorithm
Java Program to Implement ConcurrentLinkedQueue API
String Initialization in Java
Quick Guide to Spring Controllers
Java Program to Implement Pagoda
New Features in Java 8
Java Program to Implement Variable length array
Getting Started with Custom Deserialization in Jackson
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Java Program to Find Path Between Two Nodes in a Graph
Java Program to Represent Linear Equations in Matrix Form
Display Auto-Configuration Report in Spring Boot
Java Program to Implement ScapeGoat Tree
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Java Program to Implement PrinterStateReasons API
Java Program to Implement Queue