DevOpsSauce
DevOpsSauce

Reputation: 1387

How to print duplicates in string array only once

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.

Upvotes: 1

Views: 3803

Answers (4)

Andreas
Andreas

Reputation: 159215

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).

Upvotes: 0

Bruno Lopes
Bruno Lopes

Reputation: 191

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!!

Upvotes: 0

augray
augray

Reputation: 3171

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!

Upvotes: 0

Keith
Keith

Reputation: 3151

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.

Upvotes: 3

Related Questions