In a Meta event pipeline, duplicate records may arrive for the same event ID. Write a function that removes duplicates by event_id, keeping only the event with the largest timestamp for each ID.
Return the deduplicated events sorted by timestamp ascending. If two events have the same event_id and the same timestamp, keep the one that appears later in the input.
events, a list of dictionaries. Each dictionary has keys: "event_id" (string), "timestamp" (integer), and "payload" (string).timestamp ascending.Example 1
Input:
events = [
{"event_id": "a1", "timestamp": 100, "payload": "view"},
{"event_id": "b2", "timestamp": 90, "payload": "click"},
{"event_id": "a1", "timestamp": 105, "payload": "view_retry"}
]
Output:
[
{"event_id": "b2", "timestamp": 90, "payload": "click"},
{"event_id": "a1", "timestamp": 105, "payload": "view_retry"}
]
a1 appears twice, so we keep the later timestamp 105.
Example 2
Input:
events = [
{"event_id": "x", "timestamp": 5, "payload": "old"},
{"event_id": "x", "timestamp": 5, "payload": "new"}
]
Output:
[
{"event_id": "x", "timestamp": 5, "payload": "new"}
]
The timestamps are equal, so the later input record is kept.
0 <= len(events) <= 10^5event_id is a non-empty string0 <= timestamp <= 10^9event_id, timestamp, and payload