-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
37 lines (34 loc) · 983 Bytes
/
Solution.cs
File metadata and controls
37 lines (34 loc) · 983 Bytes
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
using System.Text;
namespace LeetCode.Problem1748{
//1748. Sum of Unique Elements
//https://leetcode.com/problems/sum-of-unique-elements/
/*
You are given an integer array nums. The unique elements of an array are the elements that appear exactly once
in the array.
Return the sum of all the unique elements of nums.
*/
public class Solution {
public int SumOfUnique(int[] nums) {
int ans = 0;
HashSet<int> dic = new HashSet<int>();
HashSet<int> duplicates = new HashSet<int>();
for (int i = 0; i < nums.Length; i++)
{
if (duplicates.Contains(nums[i]))
continue;
if (dic.Contains(nums[i]))
{
ans -= nums[i];
dic.Remove(nums[i]);
duplicates.Add(nums[i]);
}
else
{
ans += nums[i];
dic.Add(nums[i]);
}
}
return ans;
}
}
}