Jump to content

Help with Mode Ties in Java

Bee Bus Hardware

Hi, I have an assignment and part of it is I have to find the mode of an already sorted array that the user inputs. I have done the whole assignment already and I have gotten the mode to work with one issue. The issue is that it only records the first mode in the array and doesn't record any other if there is a a tie. Ill put my code below to show you what I have. If anyone can help get a code to record if there is a tie that would be awsome.

 

double maxValue = array[0];
        int maxCount = 0;
        for (int value : array) {
            int count = 0;
            for (j = 0; j < array.length; j++) {
                if (array[j] == value)
                    count++;
            }
            if (count > maxCount) {
                maxCount = count;
                maxValue = value;
            }
        }

        System.out.println("The mode is: " +maxValue);

 

Link to comment
Share on other sites

Link to post
Share on other sites

To find the mode of an array, you can use a similar approach to the one you have implemented. One way to modify your code to handle ties is to use a Map to track the count of each value in the array. You can then iterate through the map to find the value with the highest count.

 

Here is an example of how you can do this:

import java.util.HashMap;
import java.util.Map;

//Implement your methods and use this body:

double[] array = {1, 2, 3, 3, 3, 4, 5};

Map<Double, Integer> counts = new HashMap<>();
for (double value : array) {
    if (counts.containsKey(value)) {
        counts.put(value, counts.get(value) + 1);
    } else {
        counts.put(value, 1);
    }
}

int maxCount = 0;
double maxValue = 0;
for (Map.Entry<Double, Integer> entry : counts.entrySet()) {
    if (entry.getValue() > maxCount) {
        maxCount = entry.getValue();
        maxValue = entry.getKey();
    }
}

System.out.println("The mode is: " + maxValue);

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 12/16/2022 at 9:21 AM, Mokomoko said:

To find the mode of an array, you can use a similar approach to the one you have implemented. One way to modify your code to handle ties is to use a Map to track the count of each value in the array. You can then iterate through the map to find the value with the highest count.

 

Here is an example of how you can do this:

import java.util.HashMap;
import java.util.Map;

//Implement your methods and use this body:

double[] array = {1, 2, 3, 3, 3, 4, 5};

Map<Double, Integer> counts = new HashMap<>();
for (double value : array) {
    if (counts.containsKey(value)) {
        counts.put(value, counts.get(value) + 1);
    } else {
        counts.put(value, 1);
    }
}

int maxCount = 0;
double maxValue = 0;
for (Map.Entry<Double, Integer> entry : counts.entrySet()) {
    if (entry.getValue() > maxCount) {
        maxCount = entry.getValue();
        maxValue = entry.getKey();
    }
}

System.out.println("The mode is: " + maxValue);

 

Thanks, this worked

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×