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.

Results 1 to 4 of 4

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

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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;
            }
          }
        }
      }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2021
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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");
        }
      }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. question about debug mode
    By leonne in forum Java IDEs
    Replies: 5
    Last Post: May 15th, 2013, 06:18 AM
  2. Need help with RSA Encryption with ECB Mode
    By mdot in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2013, 11:10 AM
  3. Getting multi mode problem
    By JlovesJava in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 16th, 2013, 05:39 PM
  4. question about debug mode
    By leonne in forum Java Theory & Questions
    Replies: 2
    Last Post: January 9th, 2013, 04:16 PM
  5. Works on debug mode but not on run mode
    By alfonsoraul in forum Member Introductions
    Replies: 0
    Last Post: April 14th, 2010, 02:58 PM