Monday 27 November 2017

Java Program to Find the Mode in a Data Set


Code:

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:

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



More Java Programs:
















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...