Loading...
Loading...
Loading...
A store counts how often each word appears in customer reviews. Given the words, print every different word with the number of times it appears, in the order of its first appearance.
<word> <count> for each different word, in order of first appearance.Pen and pen are different wordsExample
Input
6 pen pencil pen ruler pen pencil
Output
pen 3 pencil 2 ruler 1
pen appears 3 times, pencil 2 times, ruler once; the words print in the order they first appeared.
Try the problem first. Open one hint at a time.
Use the word as the key and its count as the value: count.merge(word, 1, (a, b) -> a + b).
A HashMap would print in a random-looking order. Which map keeps insertion order?
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor