Hash map · Easy
A tag picker receives duplicate events and must display the frequency of each tag in first-seen order.
Count case-sensitive tags without changing first-appearance ordering.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ tags: string[] }
Input rules
- Empty tag strings are valid.
What each field means
tags— tag strings in display order. Case matters, and an empty string is a valid tag.
Output
Return value and rules
- Return one
{tag, count}object for each distinct tag. - Count all occurrences, but order the objects by each tag’s first appearance.
- Do not alphabetically sort the result. Empty input returns
[].
Constraints
- 0 <= tags.length <= 100000; each tag has at most 40 ASCII characters
- Do not sort tags or mutate input.
Examples
Example 1
{
"tags": [
"b",
"a",
"b"
]
}Returns
[
{
"tag": "b",
"count": 2
},
{
"tag": "a",
"count": 1
}
]Example 2
{
"tags": []
}Returns
[]Required complexity
O(n) time and O(k) space