Group By Max
MediumData EngineeringArraySorting
Description
Given an array of [key, value] pairs, return [key, max] for each distinct key, ordered by the key’s first appearance.
Examples
Input:
[[1,10],[1,30],[2,20]]Output:
[[1,30],[2,20]]Explanation:
Rows sharing a key are collapsed to that key with the largest value among them, keeping first-appearance order.
Input:
[[5,1],[5,2]]Output:
[[5,2]]Explanation:
Rows sharing a key are collapsed to that key with the largest value among them, keeping first-appearance order.
Input:
[[1,5],[2,3],[1,2]]Output:
[[1,5],[2,3]]Explanation:
Rows sharing a key are collapsed to that key with the largest value among them, keeping first-appearance order.
Constraints
- •
1 ≤ pairs ≤ 10⁴