0

Right, so I have a 2 part sorting algorithm. It's all based on an array of 14 random integers. For example:

int[] a = {9,2,4,8,9,4,3,2,8,1,2,7,2,5};

Now, the first thing I'm trying to figure out how to do is to count how many a certain number exists in the original array. So, we know that 1 exists once, and 2 exists four times in the original array. But as easy as it is to visually see this, what if we don't have access to the original array. So I need to craft a method that will count how many of each number 1-9 exists and put this in a new array called count. So that index 0 in count would represent the integer 1 and would have a value of 1. Index 1 will represent the integer 2 and have a value of 4. And so on and so forth. Here is what I've got so far but I'm stuck. Sorting is pretty challenging for me.

public static void main(String[] args)
{
  // int[] countFinal = {1,4,1,2,1,0,1,2,2}; // The number of times a number 1-9 appears in a[].
  // int[] sortedFinal = {1,2,2,2,2,3,4,4,5,7,8,8,9,9}; // What we need as a final product.
  int[] a = {9,2,4,8,9,4,3,2,8,1,2,7,2,5};
  //int[] count = {};
  int[] sorted = {};
  countHowMany(a, 1);
  countHowMany(a, 2);
  countHowMany(a, 3);
  countHowMany(a, 4);
  countHowMany(a, 5);
  countHowMany(a, 6);
  countHowMany(a, 7);
  countHowMany(a, 8);
  countHowMany(a, 9);


}

public static int countHowMany(int[] array, int value)
{
  // Gathering a count for how many times a number 1-9 exists and adding it to count[];
  int howManyCount = 0;
  for (int i = 0; i < array.length; i++)
  {
     if (array[i] == value)
     {
        howManyCount++;
     }
  }
  System.out.println(howManyCount);
  count = new int[9];
  count[howManyCount];
  System.out.println(Arrays.toString(count); // Testing the input
  return howManyCount;


 }  

It appears to count the number of times an item in the array exists properly. Now I just gotta figure out how I can add that value into a new array count[] and do it for each countHowMany(). This is the part I'm stuck on.

Once I have figured out count[] I can use it to create sorted[]. Now what sorted is supposed to do is take the data from the original array and count[] and create a new array that sorts it in ascending order and allows duplicates. So, since 1 occurs once and 2 occurs four times, the new array would be sorted[] = {1, 2, 2, 2, 2, ...}

It's a relatively small program and a small amount of integers, so it's ok that I create array's as necessary. The key being that I'm limited to using arrays and cannot use say ArrayLists for this.

Will Troll
  • 23
  • 1
  • 1
  • 3
  • possible duplicate of [Java count occurrence of each item in an array](http://stackoverflow.com/questions/8098601/java-count-occurrence-of-each-item-in-an-array) – dognose Nov 10 '14 at 21:06
  • @xuesheng For someone who's just learning about arrays, a `Map` is really not going to be helpful. It's much better to stick to the basic ideas until they're firmly cemented, and only then start using more advanced features. – chiastic-security Nov 10 '14 at 21:19

3 Answers3

2

You don't need to count each value individually. You can just iterate through the entire array and increment your counters for each element as you encounter it.

int counts = new int[20];  // Choose a value that's bigger than anything in your array.
int[] a = {9,2,4,8,9,4,3,2,8,1,2,7,2,5};    

for (int value : a) {
    counts[value]++;
}

If you don't know what the largest value in your array is likely to be, you're better to use either a Map to store the counts, or some kind of List that you increase the size of as needed.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • I did not know you could do that. I thought you had to go through it individually. A side note though, it adds a 0 as the first element in count. I assume this is because it's checking for 0 and there's no entries of it in a[]. Is there anyway to exclude this index? – Will Troll Nov 10 '14 at 21:39
  • Update: Figured that out. I changed counts[value]++; to be counts[value-1]++; and I got what I needed. Now to figure out sorting it. – Will Troll Nov 10 '14 at 21:45
  • Well, all you have to do is (1) undo the change that you made (2) iterate through the `counts` array by index, printing out each index as many times as the value in the array dictates. – Dawood ibn Kareem Nov 10 '14 at 22:11
0

You're better off just going through the array once and incrementing a counter for each value that might appear:

int counts[] = new int[10];
for (int n: array)
    counts[n]++;

That's enough to put the count for each n in counts[n]. You can then read the values out of your count[] array.

You might not have come across this syntax for a for loop over an array, by the way. It's equivalent to

int counts[] = new int[10];
for (int i=0; i<array.length; i++) {
    int n = array[i];
    counts[n]++;
}

but it's less verbose.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
0

Your method may as well be void, since you're not doing anything with the returned values of your countHowMany function. This will accomplish what you want:

public static void main(String[] args)
{
  int[] a = {9,2,4,8,9,4,3,2,8,1,2,7,2,5};

  //count the instances of each number in the array
  int[] count = new int[9];
  for(int i = 0; i < count.length; i++)
    count[i] = countHowMany(a, i+1);

  //put the values in the sorted array
  int[] sorted = new int[a.length];
  int position = 0; // stores the place in the array to put the new digit
  for(int digit = 0; digit < 9; digit++)
  {
    for(int inst = 0; inst < count[digit]; inst++)
    {
      sorted[position] = digit + 1;
      position++;
    }
  } 

  System.out.println(Arrays.toString(sorted));
}

The issue with your code is that you were trying to create the count array in each call of the countHowMany method, but this array is destroyed once the method finishes. The method calls should just return the counts, and then those returns should be put into the count array from outside the method. Note, however, that there are other ways to count the number of instances of each value, as noted by other answers.