-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathSolution.cpp
More file actions
47 lines (40 loc) · 1.02 KB
/
Solution.cpp
File metadata and controls
47 lines (40 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// https://leetcode.com/problems/sum-of-unique-elements/
//Test cases
// Example 1:
// Input: nums = [1,2,3,2]
// Output: 4
// Explanation: The unique elements are [1,3], and the sum is 4.
// Example 2:
// Input: nums = [1,1,1,1,1]
// Output: 0
// Explanation: There are no unique elements, and the sum is 0.
// Example 3:
// Input: nums = [1,2,3,4,5]
// Output: 15
// Explanation: The unique elements are [1,2,3,4,5], and the sum is 15.
// Approach
/*
We will create an array of 101 elements, all sets to 0 and proceed to store the frequency there.
Despite looping through 100 elements at each run to update result in the second loop.
*/
// Code
class Solution
{
public:
int sumOfUnique(vector<int> &nums)
{
// support variables
int result = 0, arr[101] = {};
// parsing nums
for (int n : nums)
{
arr[n]++;
}
// updating result
for (int i = 1; i < 101; i++)
{
result += arr[i] == 1 ? i : 0;
}
return result;
}
};