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:
Guide to PriorityBlockingQueue in Java
Explain about URL and HTTPS protocol
Java Program to Implement Disjoint Sets
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Java Program to Compute DFT Coefficients Directly
Wiring in Spring: @Autowired, @Resource and @Inject
Java program to Implement Tree Set
Lập trình đa luồng với CompletableFuture trong Java 8
How to Set TLS Version in Apache HttpClient
Logging in Spring Boot
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Java Program to Implement Queue using Linked List
Spring 5 Functional Bean Registration
Service Registration with Eureka
Spring Boot - Tomcat Port Number
Finding the Differences Between Two Lists in Java
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Finding Max/Min of a List or Collection
Command-Line Arguments in Java
Introduction to Liquibase Rollback
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Hướng dẫn Java Design Pattern – Strategy
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Giới thiệu SOAP UI và thực hiện test Web Service
Java Program to Solve the 0-1 Knapsack Problem
JUnit 5 @Test Annotation
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Java Program to Implement Karatsuba Multiplication Algorithm
Converting Iterator to List
Java Program to Implement Max Heap
Different Ways to Capture Java Heap Dumps
Hướng dẫn Java Design Pattern – Template Method