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:
Spring Boot - Code Structure
Composition, Aggregation, and Association in Java
HttpClient Timeout
Check if a String is a Palindrome in Java
StringBuilder vs StringBuffer in Java
Handling Errors in Spring WebFlux
Java Program to Describe the Representation of Graph using Incidence List
The XOR Operator in Java
Interface trong Java 8 – Default method và Static method
Java Program to Implement Sorted Circularly Singly Linked List
Examine the internal DNS cache
A Guide to Concurrent Queues in Java
Chuyển đổi từ HashMap sang ArrayList
Java Program to Implement Coppersmith Freivald’s Algorithm
Java Program to Represent Graph Using Adjacency List
Remove HTML tags from a file to extract only the TEXT
Spring MVC Setup with Kotlin
Iterating over Enum Values in Java
Spring Security OAuth Login with WebFlux
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Simple Single Sign-On with Spring Security OAuth2
Explain about URL and HTTPS protocol
JUnit 5 @Test Annotation
Compact Strings in Java 9
Uploading MultipartFile with Spring RestTemplate
Java Program to Implement Cubic convergence 1/pi Algorithm
Spring 5 Testing with @EnabledIf Annotation
Java Program to Represent Graph Using Incidence Matrix
Introduction to Using FreeMarker in Spring MVC
So sánh ArrayList và Vector trong Java
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
The Registration API becomes RESTful