Merge Similar Items

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.
I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.
You are given two 2D integer arrays, items1 and items2, representing two sets of items. Each array items has the following properties:
items[i] = [value<sub>i</sub>, weight<sub>i</sub>]wherevalue<sub>i</sub>represents the value andweight<sub>i</sub>represents the weight of thei<sup>th</sup>item.The value of each item in
itemsis unique.
Return a 2D integer array ret where ret[i] = [value<sub>i</sub>, weight<sub>i</sub>], with weight<sub>i</sub> being the sum of weights of all items with value value<sub>i</sub>.
Note: ret should be returned in ascending order by value.
LeetCode Problem - 2363
class Solution {
public List<List<Integer>> mergeSimilarItems(int[][] items1, int[][] items2) {
// Initialize the result list which will hold lists of merged items
List<List<Integer>> result = new ArrayList<>();
// Initialize a map to keep track of item values and their corresponding weights
Map<Integer, Integer> mp = new HashMap<>();
// Iterate over each item in items1
for(int i = 0; i < items1.length; i++) {
int value = items1[i][0], weight = items1[i][1];
// Put the value and weight in the map
mp.put(value, weight);
}
// Iterate over each item in items2
for(int i = 0; i < items2.length; i++) {
int value = items2[i][0], weight = items2[i][1];
// Update the map: if the value already exists, add the weight; otherwise, put the value and weight
mp.put(value, mp.getOrDefault(value, 0) + weight);
}
// Iterate over the keys in the map
for(int e : mp.keySet()) {
// Create a list to hold the current value and its total weight
List<Integer> list = new ArrayList<>();
int temp = mp.get(e);
list.add(e);
list.add(temp);
// Add the list to the result
result.add(list);
}
// Sort the result list based on the item values
Collections.sort(result, new Comparator<List<Integer>>() {
@Override
public int compare(List<Integer> o1, List<Integer> o2) {
return o1.get(0).compareTo(o2.get(0));
}
});
// Return the sorted result list
return result;
}
}




