Search code examples
javaarraysintegermode

Finding multiple modes in an array of integers with 1000 elements


So I need a way to find the mode(s) in an array of 1000 elements, with each element generated randomly using math.Random() from 0-300.

int[] nums = new int[1000];

    for(int counter = 0; counter < nums.length; counter++)
        nums[counter] = (int)(Math.random()*300);

int maxKey = 0;
    int maxCounts = 0;

    sortData(array);
    int[] counts = new int[301];

    for (int i = 0; i < array.length; i++)
    {
        counts[array[i]]++;
        if (maxCounts < counts[array[i]]) 
        {
            maxCounts = counts[array[i]];
            maxKey = array[i];
        }
    }

This is my current method, and it gives me the most occurring number, but if it turns out that something else occurred the same amount of times, it only outputs one number and ignore the rest.

WE ARE NOT ALLOWED TO USE ARRAYLIST or HASHMAP (teacher forbade it)

Please help me on how I can modify this code to generate an output of array that contains all the modes in the random array.

Thank you guys!

EDIT:

Thanks to you guys, I got it:

private static String calcMode(int[] array)
{
    int[] counts = new int[array.length];
    for (int i = 0; i < array.length; i++) {
        counts[array[i]]++;
    }
    int max = counts[0];
    for (int counter = 1; counter < counts.length; counter++) {
        if (counts[counter] > max) {
            max = counts[counter];
        }
    }

    int[] modes = new int[array.length];

    int j = 0;
    for (int i = 0; i < counts.length; i++) {
        if (counts[i] == max)
            modes[j++] = array[i];
    }

    toString(modes);
    return "";
}

public static void toString(int[] array)
{
    System.out.print("{");
    for(int element: array)
    {
        if(element > 0)
            System.out.print(element + " ");
    }
    System.out.print("}");
}

Solution

  • Look at this, not full tested. But I think it implements what @ajb said:

    private static int[] computeModes(int[] array)
    {
        int[] counts = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            counts[array[i]]++;
        }
        int max = counts[0];
        for (int counter = 1; counter < counts.length; counter++) {
            if (counts[counter] > max) {
                max = counts[counter];
            }
        }
    
        int[] modes = new int[array.length];
    
        int j = 0;
        for (int i = 0; i < counts.length; i++) {
            if (counts[i] == max)
                modes[j++] = array[i];
        }
    
        return modes;
    }
    

    This will return an array int[] with the modes. It will contain a lot of 0s, because the result array (modes[]) has to be initialized with the same length of the array passed. Since it is possible that every element appears just one time.

    When calling it at the main method:

    public static void main(String args[])
    {
        int[] nums = new int[300];
    
        for (int counter = 0; counter < nums.length; counter++)
            nums[counter] = (int) (Math.random() * 300);
    
        int[] modes = computeModes(nums);
        for (int i : modes)
            if (i != 0) // Discard 0's
                System.out.println(i);
    }