saksham goyal saksham goyal - 1 year ago 66
Java Question

find the mode number in an array

the question is this: Write a Java program to present the mode of an integer data set. Data points are input by the user from keyboard.

i want to know how to do the mode number of the array. i have no idea how to do this. hopefully you guys can help. please put comments into your code so it is easier for me and anyone else with this issue

Gaurav Mahindra has answered my problem. the solution is below.

Answer Source

Here you are :-

import java.util.Scanner;

public class FindM {
    public static void main(String[] args) {
        Scanner in=new Scanner(;
        int[] num=new int[10];
        int mode=0;

        System.out.println("Enter ten numbers");
        for(int i=0;i<10;i++){

        mode=findMode(num);//pass the array to the function
        System.out.println("The mode of given set is "+mode);


    public static int findMode(int[] num){
        int count=0;//keeps count of number of occurrences of a number
        int pos=0;//position of a number in array
        int mcount=0;//this keeps the value of maximum occurrences among all occurrences

        for(int i=0;i<num.length;i++){//loop for all numbers
            count=0;//it should be zero for each number in start
            for(int j=0;j<10;j++){//loop to compare each number with all numbers
                if(num[i]==num[j]){//compare a number with all numbers
                    count=count+1;//increase value of count if numbers match
            if(count>mcount){//compares value of count to maximum count
                    mcount=count;//assigns if count>maximum count
                    pos=i;//position of element with maximum occurrences
        return num[pos];//returns number with maximum occurrences

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download