Alright. So here's what I've currently got. Basically, I've got an array of numbers - any amount. My current code counts up the frequency of each number, then prints out the one that is the largest, and it works perfectly.

But the problem is, I need to make it be able to find multiple modes, and I have no idea on how I should try to do this. So here's a snippit of my current code:

int[] mode = new int[totalNumbers];
    int totalmode = 0;
    int tempmode = 0;
    int occur = 0;
    for (int i = 0; i < totalNumbers; i++)
    {
      tempmode = 0;
      for (int k = 0; k < totalNumbers; k++)
      {
        if (arrays[i] == arrays[k])
        {
          tempmode = tempmode + 1;
          if (totalmode < tempmode)
          {
            totalmode = tempmode;
            occur = arrays[i];
          }
        }
      }
    }

Now. TotalNumbers is an int that is the size of the array. (Pretty much the same as using arrays.length)

So, I'm not asking you to code for me, as I'm pretty sure I can do it. I just need an idea of how I can actually do it, as I'm completely stumped. Thanks!

Recommended Answers

All 9 Replies

I need to make it be able to find multiple modes

Can you explain what that means?
One way to count occurrences is to use a hashtable with the key = an occurence and the value = the count.

I'll explain using an example, here's a sample list:

5
6
7
7
8
9

In this example, there is only one mode, which is 7. The program that I posted above will find this mode easily. But, if we have another list such as:

5
6
7
7
8
8
9

There are now two modes, 7 and 8. My program will only find one of them. I need to be able to make it find both. That is what I meant.

What classes do you know how to use?
Is the range of numbers in the list small and known?
If so you could create an array to hold the counts for each number, using the number as an index to the array.

Currently that's what I've got. I don't know any of the numbers that are going to be asked. My program imports a list of numbers from a file, and stores them all into one single array.

Here's what I think about your idea, though. I'm pretty sure that it won't work. Let's use another list as an example:

5
6
6
7
7
8
8
9

Nice easy list. Now, since I don't know any of the numbers, I can just tell the program to count 1's, then 2's, 3's, and so on. (The numbers, in the end, could reach millions.) So, this is the way I thought of counting for each number: It counts by array indexes. So, in the above list, it would be looped, and counts the first array spot (arrays[0]), which is 5. It checks all the 5's, and only see's one. Then it stores 1 as the amount of first array spot. Then it moves onto the second spot - 6. Counts 2 sixes. Then the third spot...Trouble. It counts 2 sixes again, and I'll have overlap, which I'm really not sure how to overcome.

Sorry for the double post, it doesn't allow me to edit 30 minutes past. Anyways..

I think I've thought of a way to deal with the overlap. I'm going to try this.. Basically, I'm going to first sort the array with Arrays.sort(). So, with the example array of above, it will sort the modes as followed:

6
6
7
7
8
8

So, if I make a for loop, which checks the number above it, to see if it's the same. Then, if it is the same, it deletes that number, and replaces it with all the numbers above it. So in the example, if will first check 6. It see's a six right below it, so it deletes that number (when this happens, the number that is used to count will need a -1, so that it can check this number again for duplicates.) Then, it will check the next number. It will see a 6 and a 7. BINGO. Not the same, it moves on. Then, it checks again. 7 and 7. DELETE! Then it checks it again, 7 and 8, yay.

Now I've just got to make the code. I'll post back to see if it works when I do.

If you can sort the list of numbers that might help.
I missed where you count the number of occurrences of each number.
When your done, where is the list of numbers and their counts?

The list of numbers gets exported to a separate file, along with much more info.

Not sure how that will work.
Go ahead and write the code and come back if you need any more help.

Worked beautifully. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.