Deduplicate change stream

Write codeMedium25 minDatabricks and Snowflake style

Your task

Deduplicate a change-data stream keeping the latest version per key. You are given events as [key, version, payload] where key is a string, version is an int (higher = newer), and payload is a string. Multiple events may share a key. Produce the materialized table: for each distinct key, keep only the event with the highest version (versions are unique within a key). Return the surviving events as [key, version, payload] sorted ascending by key.

Examples

dedup_latest(events: list[list]) -> list[list]
Example 1
Inputdedup_latest([["a", 1, "old"], ["a", 3, "new"], ["b", 2, "x"]])
Output[["a", 3, "new"], ["b", 2, "x"]]