Results 1 to 20 of 24
Thread: Finding the Mode in An Array
 09092008, 01:56 AM #1Member
 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 [16]; array[0] = 0; array[1] = 0; array[2] = 10; array[3] = 5; array[4] = 10; array[5] = 0; array[6] = 7; array[7] = 1; array[8] = 0; array[9] = 6; array[10] = 0; array[11] = 10; array[12] = 3; array[13] = 0; array[14] = 0; array[15] = 1; System.out.println("The maximum is " + CalculateMaximum(array) + "."); } public static int CalculateMaximum(int[] array) { int maximum = array[0]; for (int i = 1; i < array.length; i++) { if ( array[i] > maximum) { maximum = array[i]; } } return maximum; } }
That was confusing. Let me know if you need clarification. Thanks

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.
 09092008, 02:20 AM #3Member
 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 010, 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?

To find the algorithm, walk away from the computer, pull out a few sheets of paper, write down 16 numbers ranging from 010 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.
 09092008, 03:53 AM #5Member
 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 :)
 09092008, 04:01 AM #6Member
 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 suggestionsbut 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 selfaccomplishment is gone. Thank you though!
 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 selfaccomplishment is gone.
 09092008, 04:39 AM #8Member
 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. :)
 09092008, 05:05 AM #9
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,372
 Blog Entries
 1
 Rep Power
 19
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?
 09092008, 04:17 PM #10Member
 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.
 09092008, 05:09 PM #11Senior Member
 Join Date
 Aug 2008
 Posts
 384
 Rep Power
 6
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[0]) { toReturn[0] = array[i]; toReturn[a++] = i; } } return toReturn; } }
I die a little on the inside...
Every time I get shot.
 09102008, 11:21 AM #12
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,372
 Blog Entries
 1
 Rep Power
 19
 10292010, 11:39 AM #13Member
 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.length1; 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.length1; j++)
if(data1[j]>data1[j+1]){
temp=data1[j];
data1[j]=data1[j+1];
data1[j+1]=temp;
}
I hope this will help
 10292010, 11:51 AM #14Member
 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
 10292010, 07:32 PM #15Member
 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.length1; j++){
count=0;
if(data1[j]==data1[j+1]){
count++;
if(count>tempcount){
tempcount = count;
result = data1[j];
}
}
}Last edited by amro; 10292010 at 07:44 PM.
 10292010, 08:51 PM #16Member
 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; 10292010 at 10:03 PM. Reason: repeated
 10292010, 08:53 PM #17Member
 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:
 10292010, 09:22 PM #18Member
 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.length1; 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;
}
 10292010, 09:46 PM #19Member
 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 length1
 10292010, 09:55 PM #20Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
Similar Threads

Finding the largest number in an array
By starchildren3317 in forum New To JavaReplies: 14Last Post: 11032010, 06:49 AM 
Disable Drag mode of Jinternal Frame
By smartsubroto in forum New To JavaReplies: 4Last Post: 06232008, 07:21 AM 
Finding elements in a vector
By Java Tip in forum java.langReplies: 0Last Post: 04142008, 08:37 PM 
Finding largest no
By bugger in forum New To JavaReplies: 11Last Post: 11292007, 12:49 PM 
Finding GCF in java
By lenny in forum Advanced JavaReplies: 1Last Post: 07312007, 05:41 AM
Bookmarks