Find the Mode of an Array in Java

  • Java
  • Thread starter courtrigrad
  • Start date
  • Tags
    Java Program
In summary: Mode{ public int Modes(int [] a) // method for class { int i; int last; int[] sorted = new int[ a.length + 1 ]; for( i = 0; i < a.length; i++) ++sorted[ a[i]]; // sort the array
  • #1
courtrigrad
1,236
2
How would i write a program in java such that if you input an array, it returns the mode of the array. IF there is more than one mode, return the first mode.
Here is my code so far:

any help is appreciated!

thanks
 
Technology news on Phys.org
  • #2
First sort the array.
Then think about it some more.
 
  • #3
Well, a mode is the element with the greatest frequency, right?

Do you know how to find frequency counts?


Sorting? Bah! I can do it in O(n) time!
 
  • #4
Really? I didn't know there was any way better than O(nlogn) (using a comparison algorithm).
 
  • #5
Oh, I mean I can do the problem in O(n) time, I didn't mean I can sort in O(n) time (except in special cases).



PS, courtigrad, don't worry about efficiency. Worry about figuring out a way to do it first, then worry about doing it better if you have time / interest.
 
  • #6
Code:
// Program returns the mode of an array. If there is more than one mode
// it returns the first mode.
public class Mode
{
    public int Modes(int [] a)  // method for class
    {
        int i;
        int frequency [] = new int[ a.length + 1 ];
         
        for( i = 0; i < a.length; i++) 
            ++frequency[ a[i] ];
        
            
    }
        if( frequency[ a[i] ] > frequency[ a[i + 1] ]
        {
            return a[i];
        }
 }

is this right?
 
  • #7
Do you know that a is an array of integers?

if so, suppose a is an array of 100 integers, the biggest one being 56,789.
What if the biggest one is 2,147,483,647?

Now what?
 
Last edited:
  • #8
i would have to sort it from leastest to greatest and then find the frequencies. Then check which one has highest frequency
 
  • #9
Yes, that's what i was thinking. I don't know how Hurkyl's idea would apply in that kind of situation (i.e. very large integers, strings, or other kinds of non-integer data types).

Were you told what kind of data is in the array?
 
  • #10
yes, just integers


maybe i should start like this?

Code:
 // Program returns the mode of an array. If there is more than one mode
// it returns the first mode.
public class Mode
{
    public void sort()
    {
        int i;
        for( i = 1; i < a.length; i++)
            for( i = 0; i < a.length - 1; i++)
              if( a[i] > a[ i + 1] 
              {
                  hold = a[i];
                  a[i] = a[i+1];
                  a[i+1] = hold;
              }
    }
 
Last edited:
  • #11
Well, sorting works for any kind of integers, so you know one way to do it. If you want to take it further and you have more information about what's in the array you may be able to come up with a way to do it without sorting. Suppose, for example, that you know that it's only small positive integers. Think about that (I'm not going to give it to you -- that would spoil it).
 
  • #12
I have an idea of how to do it, without sorting, in n time, for any integers, be they small or large. It would take some more advanced things, though...

Edit: It might be worth it, though, since it's supposed to return the first mode if there are duplicates. I'm betting that this array is probably already sorted, though, if I know programming classes, and assuming that this one isn't very advanced.
 
Last edited:

What is the "mode" in an array?

The mode in an array is the element that appears most frequently.

How do I find the mode of an array in Java?

You can find the mode of an array in Java by looping through the array and keeping track of the frequency of each element using a HashMap. Then, you can find the element with the highest frequency and that will be the mode.

What if there is more than one mode in the array?

If there is more than one mode in the array, it means that there are multiple elements that appear with the same highest frequency. In this case, you can either return all of the modes or choose one of them based on your specific needs.

What if the array is empty?

If the array is empty, there is no mode as there are no elements to compare frequencies. In this case, you can either return an error or handle it as per your specific needs.

Can I find the mode of an array with non-numeric elements?

Yes, the mode of an array can be found with non-numeric elements as well. However, you will need to use a different data structure and logic to keep track of the frequencies of non-numeric elements.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
493
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top