Find the Mode of an Array in Java

  • Context: Java 
  • Thread starter Thread starter courtrigrad
  • Start date Start date
  • Tags Tags
    Java Program
Click For Summary

Discussion Overview

The discussion revolves around finding the mode of an array in Java, focusing on programming techniques and efficiency. Participants explore various approaches, including sorting and frequency counting, while considering the implications of different data types and array characteristics.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant asks for help writing a Java program to find the mode of an array, specifying that if there are multiple modes, the first one should be returned.
  • Another participant suggests sorting the array as a preliminary step but does not elaborate further.
  • Some participants clarify that the mode is the element with the greatest frequency and question the method of finding frequency counts.
  • One participant claims they can find the mode in O(n) time, suggesting that sorting is not necessary.
  • Another participant expresses surprise at the possibility of achieving better than O(n log n) time complexity for sorting.
  • There is a discussion about the implications of the maximum value in the integer array and how it affects the approach to finding the mode.
  • One participant proposes sorting the array to find frequencies, while another questions how to apply this method to large integers or non-integer data types.
  • Another participant suggests that sorting works for any kind of integers but hints at potentially more efficient methods if additional information about the array is known.
  • One participant mentions having an idea for an O(n) solution applicable to any integers, indicating that it may involve more advanced techniques.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of sorting and the efficiency of various methods to find the mode. There is no consensus on the best approach, and multiple competing views remain throughout the discussion.

Contextual Notes

Some participants note the potential limitations of their proposed methods based on the characteristics of the input data, such as the range of integer values and the possibility of duplicates.

courtrigrad
Messages
1,236
Reaction score
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
First sort the array.
Then think about it some more.
 
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!
 
Really? I didn't know there was any way better than O(nlogn) (using a comparison algorithm).
 
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.
 
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?
 
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:
i would have to sort it from leastest to greatest and then find the frequencies. Then check which one has highest frequency
 
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:

Similar threads

Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
11
Views
3K
  • · Replies 39 ·
2
Replies
39
Views
8K
  • · Replies 3 ·
Replies
3
Views
3K