java - How to print duplicates in string array only once -


i have string array index of 25. have entered 25 elements, , i'm trying display them, however, want elements listed once, number of occurrences. far, number of occurrences correct, each iteration of array still printing multiple times. using brute force method since cannot use arraylist, map, etc. there give me hints logic of printing elements once? here method below:

    private void displayflowers(string flowerpack[]) {     // todo: display unique flowers along count of duplicates     /*      * example should      * roses - 7      * daffodils - 3      * violets - 5      */     for(int = 0; < 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 output see i'm 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 item pack.     2: remove item pack.     3: sort contents of pack.     4: search flower.     5: display flowers in pack.     0: exit flower pack interface. 

yes, typed rose 6 times, want display as:

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

i know brute force not in actual production, learning how manually force output, if o(n^2) complexity. we'll quicker stuff later.

if you're stuck using primitive arrays, create second array called uniques , each time come across new value, grow array adding new value it. iterate each index in flowerpack, iterate through uniques see if contains current index's value. if so, nothing, else add it. @ end, can print out contents of uniques.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -