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 LinkedBlockingDeque API
Java Program to Represent Graph Using Incidence Matrix
Guide to UUID in Java
Java Program to Generate All Possible Combinations Out of a, b, c, d, e
Toán tử instanceof trong java
Getting Started with Forms in Spring MVC
Guide to java.util.Formatter
The Spring @Controller and @RestController Annotations
LinkedHashSet trong java
Java Program to Create the Prufer Code for a Tree
Using a Mutex Object in Java
Java Program to Evaluate an Expression using Stacks
Guide to the Java Queue Interface
Java Program to Implement Find all Cross Edges in a Graph
Java Program to Implement ArrayDeque API
Java Program to Implement Hopcroft Algorithm
Java Program to implement Array Deque
Java Program to Implement Bucket Sort
Java Program to Implement Hash Tables
Jackson JSON Views
ETL with Spring Cloud Data Flow
Java Program to Generate Random Numbers Using Probability Distribution Function
Java 14 Record Keyword
Static Content in Spring WebFlux
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Spring Security and OpenID Connect
Spring Boot - Tomcat Deployment
Java Program to Perform Quick Sort on Large Number of Elements
The XOR Operator in Java
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Java Program to Implement Variable length array
A Guide to the finalize Method in Java