You are given an integer array nums
. The unique elements of an array are the elements that appear exactly once in the array.
Find the sum of all the unique elements of nums
.
Example:
Input: nums = [1,2,3,2]
Output: 4
Explanation: The unique elements are [1,3], and the sum is 4.
Approach:
C++
#include <bits/stdc++.h>using namespace std;int sumOfUnique(vector<int> &nums){map<int, int> mp;for (int i = 0; i < nums.size(); i++){mp[nums[i]]++;}int sum = 0;for (auto it = mp.begin(); it != mp.end(); it++){if (it->second == 1){sum += it->first;}}return sum;}int main(){vector<int> nums = {1, 2, 3, 2};cout << sumOfUnique(nums);return 0;}
No comments:
Post a Comment