0

I'm new to java and I have a homework assignment where I need to find the Mean, median and Mode of an Array. For some reason my code is not putting out the correct answer.

Here is the code I was provided to create the Arrays:

public static void main(String[] args) {

    int[] test01 = new int[]{2,2,2,2,2};
    int[] test01Results = new int[]{2,2,2,2};

    int[] test02 = new int[]{1,2,1,3,5,6,6,1,2,2,2,99,100};
    int[] test02Results = new int[]{2,2,17,100};

    int[] test03 = new int[]{100,200,300,400,300};
    int[] test03Results = new int[]{300,300,260,400};

    int[] test04 = new int[]{100};
    int[] test04Results = new int[]{100,100,100,100};

    int[] test05 = new int[]{100,1};
    int[] test05Results = new int[]{1,100,50,100};

Here is what I came up with to try to calculate the Mode:

public int mode() {
    int result = 0;
    // Add your code here

    int repeatAmount = 0; //the amount of repeats accumulating for the current i
    int highestRepeat=0; // the highest number of repeats so far
        for (int i=0; i<numberArray.length; i++) { 
            for (int j=i; j<numberArray.length; j++) { 
                if (i != j && numberArray[i] == numberArray[j]) { 
                    repeatAmount++;

                    if (repeatAmount>highestRepeat) { 
                        result=numberArray[i];
                    }
                    repeatAmount = highestRepeat; 
                }
                repeatAmount=0; // resets repeat Count for next comparison
            }
        }
    return result;
}

I'm getting the correct results for tests 1, 2 and 3 but getting the wrong result for Tests 4 and 5. Does anyone know what I'm doing wrong?

Thanks!

azurefrog
  • 10,785
  • 7
  • 42
  • 56
  • Possible duplicate: http://stackoverflow.com/questions/15725370/write-a-mode-method-in-java-to-find-the-most-frequently-occurring-element-in-an – prabodhprakash Sep 27 '16 at 18:20
  • 3
    he has done his homework, but facing some problems @Shadetheartist – Rishal Sep 27 '16 at 18:22
  • @rish understandable, but prabod was able to find a possible duplicate in a minute, so it shows that OP didn't put enough effort into researching his problem before posting a question. – Shadetheartist Sep 27 '16 at 18:24
  • I did see that question, but found it used a hashmap, and because I didn't know what that was, I decided to post my own question. @Shadetheartist – learningjava Sep 27 '16 at 18:26
  • His question is very specific though. He wants to know why he is getting wrong results for the last 2 tests. That's it. Dont think he needs the entire code! – Anurag Joshi Sep 27 '16 at 18:26
  • @learning [third one down](http://puu.sh/rpO9l/dd47c294a2.png) is a duplicate question and [without hashmaps.](http://stackoverflow.com/questions/7141723/javahow-do-i-find-the-mode-most-frequent-value-in-an-array-using-a-simple-fo) – Shadetheartist Sep 27 '16 at 18:32

1 Answers1

0

You never assign anything except 0 to highestRepeat. This should work:

public int mode() {
    int result = 0;
    int highestRepeat=0;
    for (int i=0; i<numberArray.length; i++) { 
        int repeatAmount = 1;
        for (int j = i + 1; j < numberArray.length; j++) { 
            if (numberArray[i] == numberArray[j]) { 
                repeatAmount++;
                if (repeatAmount > highestRepeat) { 
                    result = numberArray[i];
                    highestRepeat = repeatAmount;
                }
            }
        }
    }
    return result;
}

Some other improvements:

  • By starting the inner loop at i+1 you can skip the check if i != j.
  • By declaring repeatAmount inside the outer loop you can skip setting it to zero after the inner loop.

If you need some performance, consider using a HashMap for counting the equal array entries.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45