0

I am trying to get an array called frequencyCounters count the amount of times a number repeats in an array called array. The index is supposed to refer to the number i.e index 0 counts how many times 0 occurs and index 1 counts how many times 1 occurs. It sort of works but not perfectly.

import java.util.*;

class ArrayTest {
    public static void main(String[] args) {
        int[] array = new int[]{1, 1, 4, 4, 4, 5, 5, 5, 5, 7, 8, 8, 8, 8, 9};

        Arrays.sort(array);
        int max = array[array.length - 1];
        int[] frequencyCounter = new int[max];
        System.out.println("max is " + max);
        int checkNumber = 1;
        int index = 0;
        int length = array.length - 1;
        for (int i = 0; i <= 8; i++) {
            System.out.println("i is " + i);
            if (index == 0) {
                System.out.println("running index=0 statement");
                while (checkNumber == array[index]) {

                    if (i == 0) {
                        System.out.println("Running i==0 statement");
                        frequencyCounter[i] = frequencyCounter[i] + 1;
                    } else if (i != 0) {
                        System.out.println("Running i!=0 statement");
                        frequencyCounter[i - 1] = frequencyCounter[i - 1] + 1;
                    }

                    index = index + 1;


                }
                checkNumber = checkNumber + 1;
            } else {

                System.out.println("index is " + index);
                System.out.println("running i=0 else statement");

                while (checkNumber == array[index]) {

                    if (i == 0) {
                        System.out.println("Running i==0 statement");
                        frequencyCounter[i] = frequencyCounter[i] + 1;
                    } else {
                        System.out.println("Running i!=0 statement");
                        frequencyCounter[i - 1] = frequencyCounter[i - 1] + 1;
                    }

                    index = index + 1;
                }
                checkNumber = checkNumber + 1;
            }
            System.out.println("index is " + index + " at end of loop for");
        }
        int output = 1;
        for (int Z = 0; Z <= 8; Z++) {


            System.out.printf("The number %d repeats %d  


                    times",output,frequencyCounter[Z]             
                    output++;
        }
    }


}

For some reason index suddenly jumps from 5 to 9 and then to 14 in my output statements. I am a highschool student and would love some help on this question :) I need to find the mode(s) of the array. I am not allowed to use fancy stuff like hashmaps. I can only use the array class and loops with simple operations.

Ele
  • 33,468
  • 7
  • 37
  • 75
  • 2
    It's a pretty simple problem you're trying to solve, but your code is very complex. I'd go back to the drawing board. How would you solve this problem with a pen and paper? – John3136 Dec 10 '17 at 22:18

2 Answers2

1

You could create an array that has the size of the maximum int (+1) in the original array, loop that, count the number of occurences of each int and put the count on the index.

public static void main(String[] args) {
    int [] array=new int[] {1,1,4,4,4,5,5,5,5,7,8,8,8,8,9};
    // replace the next line with sth else to get the max of the array.
    int max = Arrays.stream(array).max().getAsInt();
    int[] res = new int[max + 1];
    for (int i = 0; i < res.length; i++) {
        res[i] = 0;
        for (int anArray : array) {
            res[i] += anArray == i ? 1 : 0;
        }
    }
    /* for (int re : res) {
        System.out.println(re);
    } */
}
baao
  • 71,625
  • 17
  • 143
  • 203
  • Read the following in the question: **I am not allowed to use fancy stuff like hashmaps**. – Ele Dec 10 '17 at 22:43
  • 1
    @EleazarEnrique - Edited the question to reflect your concerns regarding how to find the max of an array (which has close to nothing to do with the problem and can be easily replaced). Also, I'm not using hashmaps – baao Dec 10 '17 at 22:45
  • thank you this code does it much simpler then what i had. I am not that advanced into programming yet. I was wondering what the for loops ate doing and what does int anArray : array: do? and I'm guessing the ? 1 : 0 is sort of an if statement? – fsxflyer789 Productions Dec 10 '17 at 22:57
  • The outer loop just loops the resulting array and sets the default value of zero occurrences, the inner loop adds 1 if the number in the original array is the same as the index. It’s the same as if(anArray==i){res[i]+=1;} @fsxflyer789Productions – baao Dec 10 '17 at 23:00
  • for (int anArray : array) { res[i] += anArray == i ? 1 : 0; } what does int anArray:array mean (sorry I'm new to these kinds of things so a little insight to the semicolon would be great since I'm just used to using ; instead) – fsxflyer789 Productions Dec 10 '17 at 23:09
0

When you sort the array you're already halfway there. Have you thought about something like

// sort your array as you've done

int previousElement = array[0];
int curCount = 1;
int[] tmp;
int[] result;

for (int i = 1; i < array.length; i++) {
    curElement = array[i];
    if (curElement == previousElement) {
        curCount++;
    } else {
        // curCount will be the index and curElement its value
        // look into using tmp and result to get result[curCount] = curElement for all the indices necessary
        curCount = 1;
    }
}

Also see Make copy of array Java

geco17
  • 5,152
  • 3
  • 21
  • 38