Hash map · Easy
A notification consumer receives corrected payloads under the same ID. Display order should follow first arrival while contents use the last update.
Deduplicate by ID using the last payload and the first occurrence order.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ events:{id:string,value:integer}[] }
What each field means
events— notification records in arrival order. Each contains anidand integervalue.
Output
Return value and rules
- Return one
{id, value}object per distinct ID. - Use the last value received for that ID.
- Order IDs by their first appearance, not their last update.
Constraints
- 0 <= events.length <= 100000
- IDs are case-sensitive ASCII strings up to 40 characters; values in [-1000000,1000000].
Examples
Example 1
{
"events": [
{
"id": "a",
"value": 1
},
{
"id": "b",
"value": 2
},
{
"id": "a",
"value": 3
}
]
}Returns
[
{
"id": "a",
"value": 3
},
{
"id": "b",
"value": 2
}
]Example 2
{
"events": []
}Returns
[]Required complexity
O(n) time and O(u) space