Group By Min

MediumData EngineeringArraySorting

Description

Given an array of [key, value] pairs, return [key, min] for each distinct key, ordered by the key’s first appearance.

Examples

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

Rows sharing a key are collapsed to that key with the smallest value among them, keeping first-appearance order.

Input:[[5,9],[5,2]]
Output:[[5,2]]
Explanation:

Rows sharing a key are collapsed to that key with the smallest value among them, keeping first-appearance order.

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

Rows sharing a key are collapsed to that key with the smallest value among them, keeping first-appearance order.

Constraints

  • 1 ≤ pairs ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.