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:
Send email with JavaMail
Java Program to Implement Hash Tables with Quadratic Probing
Tính đa hình (Polymorphism) trong Java
Giới thiệu luồng vào ra (I/O) trong Java
Kết hợp Java Reflection và Java Annotations
Guide to the Java Clock Class
Java 8 and Infinite Streams
Spring Autowiring of Generic Types
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Why String is Immutable in Java?
Java Program to Check Whether Topological Sorting can be Performed in a Graph
Spring Data – CrudRepository save() Method
Spring @RequestParam Annotation
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Using a List of Values in a JdbcTemplate IN Clause
Inheritance with Jackson
Concatenating Strings In Java
Java 8 Streams peek() API
Java Program to Implement a Binary Search Tree using Linked Lists
Java Program to Implement Sorted Circular Doubly Linked List
Spring Boot - Hystrix
LinkedList trong java
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Use Liquibase to Safely Evolve Your Database Schema
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Java TreeMap vs HashMap
Java Program to Implement Min Hash
Java – Reader to Byte Array
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Java Program to Implement Sorted Vector
A Guide to the ResourceBundle