Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: How do I find the mode in a list when values of similar frequencies mean that there is no mode?

1. ## How do I find the mode in a list when values of similar frequencies mean that there is no mode?

My logic in this program was to compare values and store the values with the highest frequencies using maxFreq. If I have two of the same highest frequencies, then the computer should display "No mode", but I got very confused after a while because I might have several modes and I think that I might even have to use extra loops to loop through the frequencies of values to compare the frequencies of the modes.

```  public static void printStatistics(ArrayList<Integer> list) {
double sum = 0;
double maxFreq = 0;
int freq = 1;
for (int i = 0; i < list.size(); i++) {
sum += list.get(i);
for (int j = i + 1; j < list.size(); j++) {
//compare the max freq and freq count
if (list.get(i) == list.get(j)) {
freq++;
if (maxFreq < freq) {
maxFreq = freq;
freq = 1;
} else {
freq = 1;
}
}
}
}
}```  Reply With Quote

2. ## Re: How do I find the mode in a list when values of similar frequencies mean that there is no mode?

What questions do you have about java programming?
You text talks about some algorithm that you have tried to write code for.
What problem are you trying to solve? What is wrong with the program's output? What should the output be?

Do you have the algorithm for solving that problem? If so please post it so its logic can be compared to the posted code.

Why is the method named: printStatistics? I don't see any print statements.
Why are the local variable double instead of int?  Reply With Quote

3. ## Re: How do I find the mode in a list when values of similar frequencies mean that there is no mode?

I have questions on sorting algorithms, traversing 2d arrays to print the sums, moving elements around, and understanding the super keyword/is a-has a relationship/class casting. These areas are making me struggle since I don't understand why my code doesn't work in certain situations.

The assignment requires me to find the sum, average, and mode of a list of integers passed into the array. I can find the average and sum, but not the mode. My code doesn't work when there are two values with the same frequency e.g. 1, 1, 3, 3, 5, 5 (my code would print 5 instead of "There is no mode").

Here's my full code:

```public static void printStatistics(ArrayList<Integer> list) {
int sum = 0;
int maxFreq = 0;
int freq = 1;
boolean noSimilarMode = true;
ArrayList<Integer> l1 = new ArrayList<Integer>();
for (int i = 0; i < list.size(); i++) {
sum += list.get(i);

for (int j = 0; j < list.size(); j++) {
//compare the max freq and freq count
if (j == i) {
continue;
}
if (list.get(i) == list.get(j)) {
freq++;
if (j < list.size() - 1 && maxFreq < freq && (list.get(j + 1) != list.get(j))) {
maxFreq = list.get(j);
l1.add(freq);
freq = 1;
}
} else {
freq = 1;
}
if (maxFreq < freq) {
maxFreq = list.get(j);
freq = 1;
} else {
freq = 1;
}
}
}

for (int a = 0; a < l1.size(); a++) {
for (int b = 0; b < l1.size(); b++) {
if (l1.size() > 1 && (a == b)) {
continue;
} else {
if (l1.get(a) == l1.get(b)) {
noSimilarMode = false;
break;
}
}
}
}

double avg = 1.0 * sum/(list.size()); //avg
System.out.println();
// prints
if (maxFreq > 1 && freq == 1 && noSimilarMode) {
System.out.println("Sum: " + sum);
System.out.println("Average: " + avg);
System.out.println("Mode: " + maxFreq);
}
}*/ else if (noSimilarMode == false || freq == 1) {
System.out.println("Sum: " + sum);
System.out.println("Average: " + avg);
System.out.println("Mode: no single mode");
}
}
}```  Reply With Quote

4. ## Re: How do I find the mode in a list when values of similar frequencies mean that there is no mode?

The posted code does not compile. Please fix the compiler errors.

How are you trying to debug the code to see what is wrong?  Reply With Quote