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:
New Features in Java 12
Guide to CopyOnWriteArrayList
Configure a RestTemplate with RestTemplateBuilder
Java Program to Implement the Monoalphabetic Cypher
Jackson JSON Views
Các kiểu dữ liệu trong java
The Spring @Controller and @RestController Annotations
Transaction Propagation and Isolation in Spring @Transactional
Overview of Spring Boot Dev Tools
Spring Boot - Interceptor
Java Program to Implement Brent Cycle Algorithm
Hướng dẫn sử dụng Java Annotation
Java Byte Array to InputStream
Request Method Not Supported (405) in Spring
Hướng dẫn Java Design Pattern – Null Object
Creating a Custom Starter with Spring Boot
Check if there is mail waiting
How to Read a File in Java
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Hướng dẫn Java Design Pattern – Proxy
Spring Boot Tutorial – Bootstrap a Simple Application
How to Define a Spring Boot Filter?
Introduction to Spring Boot CLI
Introduction to Spliterator in Java
Guide to @JsonFormat in Jackson
Registration – Password Strength and Rules
Spring 5 and Servlet 4 – The PushBuilder
Spring Boot - File Handling
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
String Joiner trong Java 8
Java Program to Check the Connectivity of Graph Using DFS
How to Read HTTP Headers in Spring REST Controllers