# Thread: Finding the Mode in An Array

1. Member Join Date
Aug 2008
Posts
31
Rep Power
0

## Finding the Mode in An Array

Hi. My instructor gave us an assignment to calculate the modes in an array. Basically, the array values (not the index) contains the frequencies of the index number (like a histogram). For example, since the value of "10" (the highest number for values) occurs in index numbers 2, 4, and 11, the program must output that the "mode occurs at index numbers 2, 4, and 11." Here's what I have so far:

Java Code:
```public class Stats
{
public static void main(String[]args)
{
int[] array = new int ;
array = 0;
array = 0;
array = 10;
array = 5;
array = 10;
array = 0;
array = 7;
array = 1;
array = 0;
array = 6;
array = 0;
array = 10;
array = 3;
array = 0;
array = 0;
array = 1;

System.out.println("The maximum is  " + CalculateMaximum(array) + ".");
}
public static int CalculateMaximum(int[] array)
{
int maximum = array;
for (int i = 1; i < array.length; i++)
{
if ( array[i] > maximum)
{
maximum = array[i];
}
}
return maximum;
}
}```
So far, I've managed to calculate the maximum (the program outputs 10). My problem is, I can't think of an algorithm that will return the index numbers if the array value is its maximum. That is... what can I do so that whenever the program sees that the maximum number (the mode) is 10, that it returns the index value of the array?

That was confusing. Let me know if you need clarification. Thanks  Reply With Quote

2. ## Hint: you could use a second array whose indices are the range of numbers possible in your array, initialize each value to 0,... I'll leave the rest to you.  Reply With Quote

3. Member Join Date
Aug 2008
Posts
31
Rep Power
0

## Fubarable,

Thanks for that response. Someone told me to use a second array, and you're recommending it too! I'm still a novice... the problem still serves ambiguity. What can we achieve by creating a second array whose indices are the ranges 0-10, and initializing each array value to 0?

Are we trying to create a search algorithm, and it returns the modal indices for the original array?  Reply With Quote

4. ## To find the algorithm, walk away from the computer, pull out a few sheets of paper, write down 16 numbers ranging from 0-10 on this paper, and solve the problem on paper only. Then do it again. Once you are familiar with the steps necessary to solve this on paper, it's usually fairly easy to translate this into Java code.

Good luck.  Reply With Quote

5. Member Join Date
Apr 2008
Posts
42
Rep Power
0

## Usually when I want to solve a maximum, minimum programing thing I use Math.max(the first number, the second number);
all I have to do is just figure out a way to use it with arrays.
In your case I would put it inside a loop . using the same array (no need for another). I would need 2 loops instead. so that the first number would stay still while the second number keeps increasing. one way or another it would get the maximum. Now I still didn't try it myself and I'm just giving you an idea. If by any chance you wrote this thread because you were looking for someone to actually write the code and show it to u. I would be happy to show you my way, but if you want to do it on your own, thats fine too :)  Reply With Quote

6. Member Join Date
Aug 2008
Posts
31
Rep Power
0

## Unfortunately, my perseverance and pride forces me to solve the problem on my own with only the suggestions of others as helping tools. Thank you for both of your suggestions--but I'm afraid I need to take it a step backwards in order to be on the same page as you two.

AFAIK, by giving me code, you'll only give me answers to my problem (and I probably will not learn it as well). In addition, that quality of self-accomplishment is gone. Thank you though!  Reply With Quote

7. ## by giving me code, you'll only give me answers to my problem (and I probably will not learn it as well). In addition, that quality of self-accomplishment is gone.
Well spoken. If only more felt like you, I'd have more faith in the future of programming.  Reply With Quote

8. Member Join Date
Apr 2008
Posts
42
Rep Power
0

## Good answer bro. I thought u might be just a guy who is taking a required course even though its not your major. Good thing I just gave an idea. :)  Reply With Quote

9. ## What index you want to have if if multiple max values are found? According to your example value 10 is the maximum and it found three times in the array. So what is the required index? First occurrence or all of them?  Reply With Quote

10. Member Join Date
Sep 2008
Posts
31
Rep Power
0

## what can you do is that just search for number with highest frequency.Then,search again and compare with that no and print the index.  Reply With Quote

11. Senior Member Join Date
Aug 2008
Posts
384
Rep Power
13

## Java Code:
```public int[] getMaximum(int[] array) {
int[] toReturn = new int[array.length+1];
for (int i = 0, a = 0; i < array.length; i++)
{
if ( array[i] > toReturn)
{
toReturn = array[i];
toReturn[a++] = i;
}
}
}
}```
This returns an array with the maximum in the first index, and in the other indexes the place of the it in the array checked.  Reply With Quote

12. ##  Originally Posted by khajalid what can you do is that just search for number with highest frequency.Then,search again and compare with that no and print the index.
Yes, this is the easiest way you have to follow. That's why I ask about the maximum occurrence.  Reply With Quote

13. Member Join Date
Oct 2010
Posts
45
Rep Power
0

## Hello
I am doing a similar assignment.In a previous part of my code I have a method to arrange the data in a variable length array Arrange() .and by modifying the arrange method and add it after performing the arrange we have

public double Mode( double [] data1 )
{
this.Arrange(data1);

int count=0;
int tempcount=0;
for(int i=1; i<data1.length; i++)
for(int j=0; j<data1.length-1; j++){
count=0;
if(data1[j]==data1[j+1]){
count++;
if(count>tempcount){
tempcount = count;
result = data1[j];
}
}
}

return result;
}

and the original arrange method is :

public void Arrange( double [] data1 )
{

double temp;
for(int i=1; i<data1.length; i++)
for(int j=0; j<data1.length-1; j++)
if(data1[j]>data1[j+1]){
temp=data1[j];
data1[j]=data1[j+1];
data1[j+1]=temp;
}
I hope this will help  Reply With Quote

14. Member Join Date
Oct 2010
Posts
45
Rep Power
0

## the idea is to compare frequencies and keep the highest which is easy when the array is arranged and it is somehow similar to the bubble arrange it self  Reply With Quote

15. Member Join Date
Oct 2010
Posts
45
Rep Power
0

## but what if the data is bimodal (tow modes) or multimodal(three and above)

this code will only return the mode that will appear first (lowest value mode in ascending order or highest in descending order)
that means we can use the same code but with reversing the order and compare it with the first mode, if equal we return it.
but i think its more practical to use only this code inside new loop by starting each time at the index next to the last mode index and compare and and save equal modes

again it sames more even easier to do it without the new loop thus, by inserting another array to keep the modes
but this will create more problems for me because i designed the my Jframe to receive only a single double :(
how to insert the array in this code ??
any ideas are welcomed and appreciated

public double Mode( double [] data1 )
{
this.Arrange(data1);

int count=0;
int tempcount=0;
for(int i=1; i<data1.length; i++)
for(int j=0; j<data1.length-1; j++){
count=0;
if(data1[j]==data1[j+1]){
count++;
if(count>tempcount){
tempcount = count;
result = data1[j];
}
}
}
Last edited by amro; 10-29-2010 at 07:44 PM.  Reply With Quote

16. Member Join Date
Oct 2010
Posts
45
Rep Power
0

## any idea how to retrieve the modes and put them in an array to be returned to the caller ??
Last edited by amro; 10-29-2010 at 10:03 PM. Reason: repeated  Reply With Quote

17. Member Join Date
Oct 2010
Posts
45
Rep Power
0

## rwong

the above codes by me are wrong
i will be back after figuring out whats wrong :mad:  Reply With Quote

18. Member Join Date
Oct 2010
Posts
45
Rep Power
0

## this new code is working now for finding one mode
but if there are more than one mode it will return only the one that occur first (lower value)

public double Mode( double [] data1 )
{
this.Arrange(data1);

int count=0;
int tempcount=0;
for(int i=1; i<data1.length; i++)
for(int j=0; j<data1.length-1; j++){
if(data1[j]!= data1[j+1]){count=0;}
if(data1[j]==data1[j+1]){
count++;}

if(count>tempcount){
tempcount = count;

result = data1[j];}
}

return result;
}  Reply With Quote

19. Member Join Date
Oct 2010
Posts
45
Rep Power
0

## the last code will not work probably if the last element in the array is part of a mode and is deciding if it is the mode or not

fore example 1,1,1,4,4,4,4 will give one as the mode which is wrong
but 1,1,1,4,4,4,4,4,4 will give the four which is correct because the code knew that before reaching the last element
this is because of the length-1  Reply With Quote

20. Member Join Date
Oct 2010
Posts
45
Rep Power
0

## perhaps we should look for other arrangement methods and modify them to find the mode

any idea ?  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•