Answer: ONECARD Hiring | Merge Two 2D Arrays by Summing Values | Off-Campus OA (2022)

Answer · Posted Jun 2026

Approach Use a TreeMap: Insert all (id, value) pairs from nums1. Insert all pairs from nums2. If an ID already exists, add the value. Since TreeMap stores keys in sorted order, the final result is automatically sorted. Strategy Store merged values using a sorted map. Aggregate values for common IDs. Convert map entries into a 2D array. Java Code: import java.util.*; class Solution { public int[][] mergeArrays(int[][] nums1, int[][] nums2) { Map<Integer, Integer> map = new TreeMap<>(); for (int[] n ...

The full answer & interview discussion are available to premium members.

Log in Create a free account