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:
How to Find an Element in a List with Java
LinkedHashSet trong Java hoạt động như thế nào?
Java Program to Implement LinkedBlockingDeque API
Java Program to Perform Complex Number Multiplication
Java Program to Implement Gabow Algorithm
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Java Program to Perform Addition Operation Using Bitwise Operators
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Spring MVC + Thymeleaf 3.0: New Features
Comparing Two HashMaps in Java
Hướng dẫn Java Design Pattern – Abstract Factory
Java Program to add two large numbers using Linked List
Pagination and Sorting using Spring Data JPA
The DAO with Spring and Hibernate
Java Stream Filter with Lambda Expression
Introduction to Java 8 Streams
Java Program to Use rand and srand Functions
Java Program to Implement the Bin Packing Algorithm
Java Program to Implement Naor-Reingold Pseudo Random Function
Tính đóng gói (Encapsulation) trong java
Lập trình đa luồng với CompletableFuture trong Java 8
Spring Boot - Tomcat Port Number
Spring Boot - Google Cloud Platform
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Hướng dẫn Java Design Pattern – MVC
Handle EML file with JavaMail
Receive email by java client
Java Program to Find Inverse of a Matrix
Java Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
@DynamicUpdate with Spring Data JPA
Java Program to Perform Insertion in a 2 Dimension K-D Tree
Đồng bộ hóa các luồng trong Java