From 55e169378c730e3ae8a43ad5b81d39005352cfcc Mon Sep 17 00:00:00 2001 From: mukul96 Date: Wed, 9 Nov 2022 01:35:00 +0530 Subject: [PATCH] added Merge Similar Items problem --- Ordered Set/Merge Similar Items.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Ordered Set/Merge Similar Items.cpp diff --git a/Ordered Set/Merge Similar Items.cpp b/Ordered Set/Merge Similar Items.cpp new file mode 100644 index 0000000..dc41a80 --- /dev/null +++ b/Ordered Set/Merge Similar Items.cpp @@ -0,0 +1,26 @@ +class Solution +{ +public: + vector > mergeSimilarItems(vector > &items1, vector > &items2) + { + map mp; + for (int i = 0; i < items1.size(); i++) + { + mp[items1[i][0]] += items1[i][1]; + } + for (int i = 0; i < items2.size(); i++) + { + mp[items2[i][0]] += items2[i][1]; + } + + vector > res; + for (auto it = mp.begin(); it != mp.end(); ++it) + { + vector temp; + temp.push_back(it->first); + temp.push_back(it->second); + res.push_back(temp); + } + return res; + } +}; \ No newline at end of file