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 HashTable API
Mix plain text and HTML content in a mail
Spring MVC Custom Validation
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
How to Get the Last Element of a Stream in Java?
Giới thiệu luồng vào ra (I/O) trong Java
Spring Security and OpenID Connect
The Registration Process With Spring Security
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Java Program to Implement ArrayList API
Introduction to the Functional Web Framework in Spring 5
A Guide to TreeMap in Java
Java – Write to File
Java Program to Implement Binary Tree
So sánh HashMap và Hashtable trong Java
Recommended Package Structure of a Spring Boot Project
Java Program to Perform Searching in a 2-Dimension K-D Tree
Easy Ways to Write a Java InputStream to an OutputStream
Java Program to Describe the Representation of Graph using Adjacency List
Java Program to Implement Hash Tree
Java Program to Implement Binomial Heap
Sending Emails with Java
Java Program to Implement Word Wrap Problem
Java Program to Implement DelayQueue API
How to Read a File in Java
Java Program to Solve the Fractional Knapsack Problem
Spring Security – security none, filters none, access permitAll
Flattening Nested Collections in Java
So sánh HashMap và HashSet trong Java
Spring Boot - Apache Kafka