0

I have to write methods to find the mean, median, mode, etc given an array of numbers. I've gotten everything else worked out, but mode is giving me a hell of a time and don't know why.

The array is 22, 26, 45, 46, 49, 55, 57, 63, 63, 65, 66, 67, 67, 68, 68, 69, 70, 71, 72, 73, 75, 76, 76, 77, 77, 78, 78, 78, 79, 82, 82, 83, 84, 85, 87, 88, 88, 89, 89, 91, 92, 98, 99

It is returning 22, when the answer should be 78.

Here is my code:

public static int mode(int[] array){
    int[] countArray = new int[101];

    //counts each number
    for(int i = 0; i < array.length; i++){
      countArray[array[i]]++;
    }// end for loop

    int mode = array[0], modeIndex = 0;

    //finds which number occurs the most
    System.out.println(Arrays.toString(countArray));
    for(int i = 1; i < array.length; i++){
      if(countArray[i] > mode){

        mode = countArray[i];
        modeIndex = i;
        System.out.println(mode + " " + modeIndex);
      }// end if
    }// end for loop

    return modeIndex;
}// end method mode
morgano
  • 17,210
  • 10
  • 45
  • 56
PsylentKnight
  • 149
  • 14
  • 2
    There's quite a few things wrong, try debugging your code, step through it and the problems should become obvious. – Taylor Jul 02 '14 at 03:10
  • 2
    [this answer](http://stackoverflow.com/a/15725452/2817802) is what you are looking for – Baby Jul 02 '14 at 03:15

3 Answers3

1

You have a bug, the two lines that you need to change are:

if(countArray[i] > mode){
    mode = countArray[i];

to:

if(countArray[array[i]] > mode){
    mode = array[i];
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • Thank you, I actually worked on this for like an hour before posting and then figured it out almost immediately. Sorry for not coming back and letting everyone know. Thanks anyway though. – PsylentKnight Jul 02 '14 at 23:38
1

Your mistake is: int mode = array[0], there should be int mode = countArray[0] And in the cycle you should use size of countArray, not array. That code work correctly (result is 78)

 public static int mode(int[] array){
    int[] countArray = new int[101];

    //counts each number
    for(int i = 0; i < array.length; i++){
      countArray[array[i]]++;
    }// end for loop

    int mode = countArray[0], modeIndex = 0;

    //finds which number occurs the most
    System.out.println(Arrays.toString(countArray));
    for(int i = 1; i < countArray.length; i++){
      if(countArray[i] > mode){

        mode = countArray[i];
        modeIndex = i;
        System.out.println(mode + " " + modeIndex);
      }// end if
    }// end for loop

    return modeIndex;
}// end method mode`
FruitDealer
  • 164
  • 1
  • 11
1

Another way would be using the HashMap

Code :

     int arraya[] = {22, 26, 45, 46, 49, 55, 57, 63, 63, 65, 66, 67, 67, 68, 68, 69, 70, 71, 72, 73, 75, 76, 76, 77, 77, 78, 78, 78, 79, 82, 82, 83, 84, 85, 87, 88, 88, 89, 89, 91, 92, 98, 99};
    List<Integer> array = new ArrayList<>();
    for (int i = 0; i < arraya.length; i++) {
        array.add(arraya[i]);
    }

    HashMap<Integer, Integer> count = new HashMap<>();

    for (Integer i : array) {
        if (count.containsKey(i)) {
            int c = count.get(i);
            count.put(i, c + 1);
        } else {
            count.put(i, 1);
        }
    }

    List listOfOccurance = new ArrayList();
    Set<Map.Entry<Integer, Integer>> entrySet = count.entrySet();
    for (Map.Entry<Integer, Integer> entry : entrySet) {
        System.out.println(entry.getKey() + " = " + entry.getValue());
        listOfOccurance.add(entry.getValue());

    }
    Integer i = (Integer) Collections.max(listOfOccurance);

    while (it.hasNext()) {
        Map.Entry pairs = (Map.Entry) it.next();
        if ((Integer) pairs.getValue() == i) {
            System.out.println(pairs.getKey());
            return;
        }
        it.remove(); // avoids a ConcurrentModificationException
    }

Output:

65 = 1 66 = 1 67 = 2 68 = 2 69 = 1 70 = 1 71 = 1 72 = 1 73 = 1 75 = 1 76 = 2 77 = 2 78 = 3 79 = 1 82 = 2 83 = 1 84 = 1 85 = 1 22 = 1 87 = 1 88 = 2 89 = 2 26 = 1 91 = 1 92 = 1 98 = 1 99 = 1 45 = 1 46 = 1 49 = 1 55 = 1 57 = 1 63 = 2 
The mode is 78
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58