Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7537371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:50:04+00:00 2026-05-30T06:50:04+00:00

Does anyone know how to find the modes in an array when there are

  • 0

Does anyone know how to find the modes in an array when there are more then one mode? I have this code that finds one mode. But I’m dealing with an array which has more than one mode, a multimodal array and I have to print each mode exactly once. Here is my code, can someone help me out? Thanks.

public static int mode(int a[])
{
    int maxValue=0, maxCount=0;

    for (int i = 0; i < a.length; ++i)
    {
        int count = 0;
        for (int j = 0; j < a.length; ++j)
        {
            if (a[j] == a[i]) ++count;
        }

        if (count > maxCount)
        {
            maxCount = count;
            maxValue = a[i];
        }
    }

    return maxCount;
}

public static Integer[] modes(int a[])
{
    List<Integer> modes = new ArrayList<Integer>();
    int maxCount=0;
    for (int i = 0; i < a.length; ++i)
    {
        int count = 0;
        for(int j = 0; j < a.length; ++j)
        {
            if (a[j] == a[i]) ++count;
        }

        if (count > maxCount)
        {
            maxCount = count;
            modes.clear();
            modes.add(a[i]);
        }
        else if (count == maxCount)
        {
            modes.add(a[i]);
        }
    }
    return modes.toArray(new Integer[modes.size()]);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:50:06+00:00Added an answer on May 30, 2026 at 6:50 am

    Since your elements will be between 10 and 1000, you can use a Counter array. In this Counter array, you can store the counts of the value of the a[i] element. I think you can understand this better in code:

    public static List<Integer> mode(int[] a) {
        List<Integer> lstMode = new ArrayList<Integer>();
        final int MAX_RANGE = 1001;
        int[] counterArray = new int[MAX_RANGE]; //can be improved with some maths :)!
        //setting the counts for the counter array.
        for (int x : a) {
            counterArray[x]++;
        }
        //finding the max value (mode).
        int maxCount = counterArray[0];
        for(int i = 0; i < MAX_RANGE; i++) {
            if (maxCount < counterArray[i]) {
                maxCount = counterArray[i];
            }
        }
        //getting all the max values
        for(int i = 0; i < MAX_RANGE; i++) {
            if (maxCount == counterArray[i]) {
                lstMode.add(new Integer(i));
            }
        }
        return lstMode;
    }
    

    If your input will have elements outside of 1000, you can look for the Map answer (like in other posts).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know where to find an explanation to the warnings in VS 2005
Does anyone know where to find a good, opensource RSS reader in Flash? I
Does anyone know (or know where to find, couldn't myself) what variables I can
Does anyone know how to find out the precision of long double on a
Does anyone know how to find the DELAY_KEY_WRITE value for a specific table? It's
Does anyone know of where to find unit testing guidelines and recommendations? I'd like
Does anyone know where I can find a library of common but difficult (out
Does anyone know how to programmaticly find out where the java classloader actually loads
Does anyone know where I can find an example of how to determine if
Does anyone know a way to find out programatically which physical disk holds a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.