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:
Zipping Collections in Java
Truyền giá trị và tham chiếu trong java
Java Program to Implement ArrayDeque API
Java Program to Implement the RSA Algorithm
Java Program to Implement Interpolation Search Algorithm
Prevent Brute Force Authentication Attempts with Spring Security
Converting between an Array and a List in Java
Using the Not Operator in If Conditions in Java
Runnable vs. Callable in Java
Java Program to Perform Searching Using Self-Organizing Lists
Java Program to Implement Jarvis Algorithm
Java Program to Implement Ternary Tree
Java Program to Represent Graph Using Incidence List
Extract links from an HTML page
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Spring MVC Async vs Spring WebFlux
Lớp TreeMap trong Java
Generating Random Numbers in a Range in Java
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
Mảng (Array) trong Java
Java – File to Reader
The Registration Process With Spring Security
A Guide to Apache Commons Collections CollectionUtils
Introduction to Java 8 Streams
Java Program to Implement Stack API
Spring Boot - Code Structure
A Guide to Queries in Spring Data MongoDB
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Java Program to Implement Adjacency Matrix
Java Program to find the peak element of an array using Binary Search approach
Java Program to Implement Quick sort
Java Program to Solve Travelling Salesman Problem for Unweighted Graph