Find Mode of Numbers
I have a program that collects 10 numbers from the user and stores them in an array. I need to figure out the mode, or number that appear most often, of this array and display it to the user. The only way I could think of would be a ridiculous series of 'if()' statements. But I thought there must be a more efficient way to do this.
Thanks!
[356 byte] By [
xxjtfxxa] at [2007-11-26 14:57:32]

Add your numbers to a Map where the number is the key and the valuethe frequency of your numbers. After adding all numbers a linear scanwill give you the number with the highest frequency.kind regards,Jos
If the numbers entered have to be in a range (for instance 1-10), you can try to make an array which keeps the total amount that digit is entered..
Something like this:
(pseudocode, no Netbeans here, and don't wanna make faulty progs)
initialize array_with_digits
initialize array_with_totals to 0;
for (int i = 1; i <= 10; i ++) {
get number from user;
array_with_digits[i-1] = number;
array_with_totals[number] ++;
}
And if you can't use a Map because yout teacher says so, you can use an array of int's (with a size of 10) representing the frequencies of the numbers entered by the user.
int[] frequency = new int[10];
user enters 5 --> increase frequency[5-1]
user enters 8 --> ...
>int[] frequency = new int[10];>> user enters 5 --> increase frequency[5-1]> user enters 8 --> ...That was what I posted in 'pseudo', indeed..
Hey, I forgot to mention that the program needs to make use of parallel arrays. So before I post my code, here's how I explain my usage of parallel arrays. The main array is 'numbers[]' which as the title suggests, holds all the numbers the user enters. The array 'counter[]' is the same length as the 'numbers[]
array. Then it works like this: 'counter[0]' holds the number of times 'numbers[0]' is found in the entire 'numbers[]' array.
So here's my code:
public static int[] calcMode(int numbers[])
{
// declare variables
int x = 0;
int count = 0;
int index = 0;
int counter[] = new int[numbers.length];
// loop through all numbers[] values
for (int i=0;i<numbers.length;i++)
{
numbers[i] = x;
for (int z=0;z<numbers.length;z++)
{
if (numbers[z] == x)
{
count++;
}
}
counter[index++] = count;
count = 0;
return counter;
}
return counter;
}
I test this method using the values 1,1,2,2,2,3,3,3,3,4. Next I print in the 'counter[]' array. And I get this:
Key=>Value
0=>1
1=>0
2=>0
3=>0
4=>0
5=>0
6=>0
7=>0
8=>0
9=>0
So based on this, I (think) I know the following: the test is working at least the first time. But it looks like once it finds the first match, it then quits testing. I also tested the method using 1,1,1 for the first three values. This should have returned 2 in 'counter[0]' however it returned 1 - which looks like again, it tests only once. My guess is that the first 'return counter;' statement is exiting the 'for()' loop. But if I remove the first 'return counter;' I get some funky numbers as the result (1,2,3...10). Or stated differently, the result I get is 'counter[0] = 1,counter[1]=2'. Or in equation form (lol) 'counter[a]=a+1;'
Any ideas?
Thanks!
Message was edited by:
xxjtfxx
Print numbers[ i ] immediately after this line (before you start the 'z' loop):numbers[i] = x;See what happens, and try to explain it.
int x = 0;
int count = 0;
int index = 0;
int counter[] = new int[numbers.length];
// loop through all numbers[] values
for (int i=0;i<numbers.length;i++)
{
numbers[i] = x;// YOU'RE OVERWRITING YOUR INPUT (NUMBERS[X]) WITH 0 BEFORE YOU EVER LOOK AT IT
for (int z=0;z<numbers.length;z++)
{
if (numbers[z] == x)
{
count++;
}
}
counter[index++] = count;
count = 0;
return counter;// YOU END THE METHOD AFTER ONE ITERATION THROUGH THE LOOP
}
return counter;
Couple of problems, which I pointed out in the code. Instead of comparing each element of numbers to the rest of the elements in numbers, just increment the corresponding value in counter[] when you read each input.
// declare variables
int x = 0;
int count = 0;
int index = 0;
int counter[] = new int[numbers.length];
// loop through all numbers[] values
for (int i=0;i<numbers.length;i++)
{
/*numbers[i] = x;
for (int z=0;z<numbers.length;z++)
{
if (numbers[z] == x)
{
count++;
}
}
counter[index++] = count;
count = 0;
return counter;*/
counter[numbers[i]]++;
}
return counter;
Unless I've misunderstood what you're trying to do, your output should be
Key=>Value
0=>0
1=>2
2=>3
3=>4
4=>1
5=>0
6=>0
7=>0
8=>0
9=>0
right?
Yes, that does work.
The numbers in the 'counter[]' array are a little different then what I originally thought they would be. Why is 'counter[0] = 0'. Shouldn't it be equal to 1? My worry is that when I have to program the part that checks if there is more then one mode, or no mode, that since that is zero and all the others will be 1 it'll give me faulty info.
Thanks though, really appreciate it.
The value of counter[0] is 0 because there are exactly no occurences of the number 0 in your test numbers 1,1,2,2,2,3,3,3,3,4. Try it again with these test values: 0, 0, 0, 2, 2, 6, 6, 6, 7, 7. You should get
0=>3
1=>0
2=>2
3=>0
4=>0
5=>0
6=>3
7=>2
8=>0
9=>0
Does that make sense?
Yes, I understand it now. Thanks so much for the help.