Count occurrences of elements inside array? (Java)

helloMundo picture helloMundo · Jan 20, 2014 · Viewed 25.8k times · Source

I have been working on trying to figure out this algorithm for about 6 hours now and can't seem to come up with a solution. I am trying to count the occurrences of elements inside an array and may two more separate arrays. One for the unique instances, and one for how many times these instances occurs. I found some other thinks on here about array lists and hashMaps, but I am only able to use arrays.

For example, I have this array (already sorted):

{cats, cats, cats, dog, dog, fish}

I am trying to get make an array for the instances, so:

{cats, dog, fish}

And finally, how many times these instances occur:

{3, 2, 1}

Here is the code i have so far:

public void findArrs( String[] words )
{
  int counter = 1;
  for(int i = 0; i < words.length - 1; i++){
  if(!(words[i].equals(words[i+1]))){
  counter++; 
  }
 }

 String[] unique = new String[counter];
 int[] times = new int[counter];

 for(int i = 0; i < words.length; i++){

   }    
}

This is all the code I have after all my attempts.

Answer

ansible picture ansible · Jan 20, 2014

This is how it could be done using only arrays. The tricky part is you must know the number of items before the array is created. So I had to create my own function to create a bigger array. Actually two, one for the count and one for the unique values.

If you can use Vectors you will be better off. Here is it without vetors:

public class HelloWorld{

     public static void main(String []args){
        String[] initalArray;

        // allocates memory for 10 integers
        initalArray = new String[6];
        initalArray[0] = "cats";
        initalArray[1] = "cats";
        initalArray[2] = "cats";
        initalArray[3] = "dog";
        initalArray[4] = "dog";
        initalArray[5] = "fish";

        String[] uniqueValues = new String[0];
        int[] countValues = new int[0];
        for(int i = 0; i < initalArray.length; i++)
        {
            boolean isNewValue = true;
            for (int j = 0; j < uniqueValues.length; j++)
            {
                if (uniqueValues[j] == initalArray[i])
                {
                    isNewValue = false;
                    countValues[j]++;
                }
            }

            if (isNewValue)
            {
                // We have a new value!
                uniqueValues = addToArrayString(uniqueValues, initalArray[i]);
                countValues = addToArrayInt(countValues, 1);
            }
        }

        System.out.println("Results:");
        for(int i = 0; i < countValues.length; i++)
        {
            System.out.println(uniqueValues[i] + "=" +  countValues[i]);
        }
     }

     public static String[] addToArrayString(String[] initalArray, String newValue)
     {
         String[] returnArray = new String[initalArray.length+1];
         for(int i = 0; i < initalArray.length; i++)
         {
             returnArray[i] = initalArray[i];
         }
         returnArray[returnArray.length-1] = newValue;

         return returnArray;
     }

     public static int[] addToArrayInt(int[] initalArray, int newValue)
     {
         int[] returnArray = new int[initalArray.length+1];
         for(int i = 0; i < initalArray.length; i++)
         {
             returnArray[i] = initalArray[i];
         }
         returnArray[returnArray.length-1] = newValue;

         return returnArray;
     }
}

As mentioned in the comments, if we know the array is in order, then we don't need to search through the entire previous array and can just check uniqueValues directly.

public class HelloWorld{

     public static void main(String []args){
        String[] initalArray;

        // allocates memory for 10 integers
        initalArray = new String[6];
        initalArray[0] = "cats";
        initalArray[1] = "cats";
        initalArray[2] = "cats";
        initalArray[3] = "dog";
        initalArray[4] = "dog";
        initalArray[5] = "fish";

        String[] uniqueValues = new String[0];
        int[] countValues = new int[0];
        for(int i = 0; i < initalArray.length; i++)
        {
            boolean isNewValue = true;
            if (i > 0)
            {
                if (uniqueValues[uniqueValues.length-1] == initalArray[i])
                {
                    isNewValue = false;
                    countValues[uniqueValues.length-1]++;
                }
            }

            if (isNewValue)
            {
                // We have a new value!
                uniqueValues = addToArrayString(uniqueValues, initalArray[i]);
                countValues = addToArrayInt(countValues, 1);
            }
        }

        System.out.println("Results:");
        for(int i = 0; i < countValues.length; i++)
        {
            System.out.println(uniqueValues[i] + "=" +  countValues[i]);
        }
     }

     public static String[] addToArrayString(String[] initalArray, String newValue)
     {
         String[] returnArray = new String[initalArray.length+1];
         for(int i = 0; i < initalArray.length; i++)
         {
             returnArray[i] = initalArray[i];
         }
         returnArray[returnArray.length-1] = newValue;

         return returnArray;
     }

     public static int[] addToArrayInt(int[] initalArray, int newValue)
     {
         int[] returnArray = new int[initalArray.length+1];
         for(int i = 0; i < initalArray.length; i++)
         {
             returnArray[i] = initalArray[i];
         }
         returnArray[returnArray.length-1] = newValue;

         return returnArray;
     }
}