I’m sure this isn’t a hugely complex problem but I’m relatively new to java and have been puzzling with this one for a while.
Say I have an array which contains 6 string values, and lets say they are: [Apple, Banana, Banana, Banana, Orange, Orange]. I am looking to build a method that will take this array and return a string, in the case above, as: "Apple, 3×Banana, 2×Orange".
I would greatly appreciate some help with this. I’ve been trying different techniques to achieve this but fall down on the correct behavior on the first iteration of the array and when to detect a repeated value ect.
Use
Map<String, Integer>. Iterate over the array and put strings as Map key and counter as value:Now the words map contains mapping between word and its count.