Skip to content

Added leetcode-1748 solution #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Algorithms/Easy/1748_Sum of Unique Elements/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
};