#include <iostream> #include <vector> #include <unordered_map> #include <cassert> int unique_sum(const std::vector<int>& nums) { std::unordered_map<int, int> hashMap; for (int i = 0; i < nums.size(); i++) { if (hashMap.find(nums[i]) == hashMap.end()) { hashMap[nums[i]] = nums[i]; } else { hashMap[nums[i]] = 0; } } int sum = 0; for (auto i: hashMap) { sum = sum + hashMap[i.first]; } return sum; }
- #include <iostream>
- #include <vector>
- #include <unordered_map>
- #include <cassert>
- int unique_sum(const std::vector<int>& nums) {
// CODE- std::unordered_map<int, int> hashMap;
- for (int i = 0; i < nums.size(); i++) {
- if (hashMap.find(nums[i]) == hashMap.end()) {
- hashMap[nums[i]] = nums[i];
- } else {
- hashMap[nums[i]] = 0;
- }
- }
- int sum = 0;
- for (auto i: hashMap) {
- sum = sum + hashMap[i.first];
- }
- return sum;
- }