1

I have a String array with an index of 25. I have entered 25 elements, and I'm trying to display them, however, I only want the elements listed once, then the number of occurrences. Thus far, the number of occurrences is correct, but each iteration of the array is still printing multiple times. I am using a brute force method since I cannot use an ArrayList, Map, etc. Is there anyone who could give me hints to the logic of only printing the elements once? Here is the method below:

 private void displayFlowers(String flowerPack[]) {
 // TODO: Display only the unique flowers along with a count of any duplicates
 /*
 * For example it should say
 * Roses - 7
 * Daffodils - 3
 * Violets - 5
 */
 for(int i = 0; i < flowerPack.length; i++) {
 int count = 0;
 for(int j = 0; j < flowerPack.length; j++) {
 if(flowerPack[i].equals(flowerPack[j]))
 {
 count++;
 }
 }
 System.out.println(flowerPack[i] + " - " + count);
 }

And here is the output to see what I'm am talking about:

 rose - 6
 daffodil - 2
 rose - 6
 daisy - 3
 tulip - 2
 wildflower - 3
 lily - 3
 lily - 3
 daisy - 3
 rose - 6
 wildflower - 3
 rose - 6
 lilac - 1
 daffodil - 2
 rose - 6
 lily - 3
 tulip - 2
 wildflower - 3
 daisy - 3
 rose - 6
 carnation - 1
 orchid - 1
 sunflower - 3
 sunflower - 3
 sunflower - 3
 1: Add an item to the pack.
 2: Remove an item from the pack.
 3: Sort the contents of the pack.
 4: Search for a flower.
 5: Display the flowers in the pack.
 0: Exit the flower pack interface.

Yes, I typed rose 6 times, but I only want it to display it as:

 rose - 6
 daffodil -2
 daisy - 3
 tulip - 2
 etc
 etc

I know brute force does not do well in actual production, but we are learning how to manually force output, even if it is O(n^2) complexity. We'll get into the quicker stuff later.

asked Aug 30, 2015 at 20:20
5
  • Why can't you use an ArrayList, Map etc.? A Set sounds like exactly what you're looking for. Commented Aug 30, 2015 at 20:23
  • 2
    Two hints: (1) You should start the inner loop at the index of the outer loop + 1. (2) Use another (boolean) array to keep track of the flowers you have counted. Commented Aug 30, 2015 at 20:26
  • Sorry, I didn't mention Set. We haven't gotten that far yet, so we're limited to the current scope of our knowledge. Commented Aug 30, 2015 at 20:34
  • If someone is going to down vote his question, perhaps they can have the decency to explain why it's been done? Commented Aug 30, 2015 at 20:42
  • @luane: I expound on this idea in my answer below, but do not encourage or mention the optimization of starting the counting loop at the index beyond the current one as this isn't necessary to achieve correct output and might risk confusing the OP. Commented Aug 30, 2015 at 21:00

4 Answers 4

3

If you're stuck only using primitive arrays, create a second array called something like uniques and each time you come across a new value, grow that array by adding the new value to it. As you iterate to each index in flowerPack, iterate through uniques to see if it already contains the current index's value. If so, do nothing, else add it. At the end, you can then print out the contents of uniques.

answered Aug 30, 2015 at 20:38

Comments

0

Since you've said you can't use any library classes, and want to stick with your basic approach, here's a slight alteration you can make to only print a given type of flower once. It also saves you on a little computation. Basically, have a secondary boolean array in which you track whether you've already printed data incorporating the flower at an index or not.

You've expressed through comments some uncertainty about booleans. A boolean is just a representation of true or false. An array is a way of holding multiple values of a single type together, with some order associated with the values. In my solution, the idea is that you have a second array where the index (the numeric value representing which item in the array you care about) of the boolean array is used to correspond to the index of the flower array. So the 0 item in the boolean array corresponds to the 0 item in the String array, the 1 item corresponds to the 1 item, the nth item to the nth item etc.

You can use this boolean array to represent whether you have printed the information about the type of flower at index n or not.

Note: Have removed the actual code of the solution in the interest of encouraging learning by figuring out the details yourself. Best of luck!

answered Aug 30, 2015 at 20:35

4 Comments

I see what the boolean array is doing. I haven't worked with that form of an array yet, but I see where you're going with it; however, I still struggle with understanding booleans. I know they're true or false, yes or no, 1 or 0, etc, I'm just trying to understand how to actually implement them in my code.
A boolean is just a representation of true or false. An array is a way of holding multiple values of a single type together, with some order associated with the values. In my solution, the idea is that you have a second array where the index (the numeric value representing which item in the array you care about) of the boolean array is used to correspond to the index of the flower array. So the 0 item in the boolean array corresponds to the 0 item in the String array, the 1 item corresponds to the 1 item, the nth item to the nth item etc.
You can use this boolean array to represent whether you have printed the information about the type of flower at index n or not.
@augray--upvote for the right attitude--removing code (that you had already written to best answer the question) to encourage learning. Plus a nice added comment.
0

To keep your code the most untouched possible, I would create a new variable like this:

private static void displayFlowers(String flowerPack[]) {
 // TODO: Display only the unique flowers along with a count of any duplicates
 /*
 * For example it should say
 * Roses - 7
 * Daffodils - 3
 * Violets - 5
 */
 int isChecked[] = new int[flowerPack.length];
 for(int i = 0; i < flowerPack.length; i++) {
 int count = 0; 
 for(int j = 0; j < flowerPack.length; j++) {
 if(flowerPack[i].equals(flowerPack[j]))
 {
 count++;
 if(i!=j)isChecked[j] = 1;
 }
 }
 if(isChecked[i]==0)
 System.out.println(flowerPack[i] + " - " + count);
 }
 }

With the array: String []flowers = new String[]{"rose","sunflower","rose","rose","sunflower","daisy","daisy"};

It prints:

rose - 3 sunflower - 2 daisy - 2

Hope that helps!!

answered Aug 30, 2015 at 20:57

Comments

0

You want to only print each word once, and to do that without using a Set, you could make sure to only print the word the first time, or the last time, it occurs. Let's assume first:

  • Loop thru words:
    • Check if word exists earlier in the list.
    • If it does not:
      • Count number of times word exists after in the list.
      • Print the word and the count (+1 since you didn't count first word itself).
answered Aug 30, 2015 at 20:30

1 Comment

Changed answer to not say "best" because it's not the most efficient way, but it is a valid way to get the results. Does this answer the unspoken down-vote?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.