Deduplicate by Latest Timestamp

MediumData EngineeringArrayMath

Description

Given an array of [key, timestamp, value] rows, keep only the row with the largest timestamp for each key (an upsert). Return the surviving rows ordered by key ascending. Timestamps are unique within a key.

Examples

Input:[[1,5,50],[2,3,30],[1,8,80]]
Output:[[1,8,80],[2,3,30]]
Explanation:

For each key only the row with the latest timestamp survives, and the kept rows are ordered by key.

Input:[[3,1,99]]
Output:[[3,1,99]]
Explanation:

For each key only the row with the latest timestamp survives, and the kept rows are ordered by key.

Input:[[1,1,10],[1,2,20],[1,3,30]]
Output:[[1,3,30]]
Explanation:

For each key only the row with the latest timestamp survives, and the kept rows are ordered by key.

Constraints

  • 1 ≤ number of rows ≤ 10⁴
  • timestamps unique within a key

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.