classSolution: defsumOfUnique(self, nums: List[int]) -> int: returnsum(num for num, cnt in Counter(nums).items() if cnt == 1)
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: intsumOfUnique(vector<int> &nums){ unordered_map<int, int> cnt; for (int num : nums) { ++cnt[num]; } int ans = 0; for (auto &[num, c] : cnt) { if (c == 1) { ans += num; } } return ans; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { publicintsumOfUnique(int[] nums) { Map<Integer, Integer> cnt = newHashMap<Integer, Integer>(); for (int num : nums) { cnt.put(num, cnt.getOrDefault(num, 0) + 1); } intans=0; for (Map.Entry<Integer, Integer> entry : cnt.entrySet()) { intnum= entry.getKey(), c = entry.getValue(); if (c == 1) { ans += num; } } return ans; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicclassSolution { publicintSumOfUnique(int[] nums) { Dictionary<int, int> cnt = new Dictionary<int, int>(); foreach (int num in nums) { if (!cnt.ContainsKey(num)) { cnt.Add(num, 0); } ++cnt[num]; } int ans = 0; foreach (KeyValuePair<int, int> pair in cnt) { int num = pair.Key, c = pair.Value; if (c == 1) { ans += num; } } return ans; } }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12
funcsumOfUnique(nums []int) (ans int) { cnt := map[int]int{} for _, num := range nums { cnt[num]++ } for num, c := range cnt { if c == 1 { ans += num } } return }
typedefstruct { int key; int val; UT_hash_handle hh; } HashEntry;
intsumOfUnique(int* nums, int numsSize){ HashEntry * cnt = NULL; for (int i = 0; i < numsSize; ++i) { HashEntry * pEntry = NULL; HASH_FIND(hh, cnt, &nums[i], sizeof(int), pEntry); if (NULL == pEntry) { pEntry = (HashEntry *)malloc(sizeof(HashEntry)); pEntry->key = nums[i]; pEntry->val = 1; HASH_ADD(hh, cnt, key, sizeof(int), pEntry); } else { ++pEntry->val; } } int ans = 0; HashEntry *curr, *next; HASH_ITER(hh, cnt, curr, next) { if (curr->val == 1) { ans += curr->key; } } HASH_ITER(hh, cnt, curr, next) { HASH_DEL(cnt, curr); free(curr); } return ans; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13
var sumOfUnique = function(nums) { const cnt = newMap(); for (const num of nums) { cnt.set(num, (cnt.get(num) || 0) + 1); } let ans = 0; for (const [num, c] of cnt.entries()) { if (c === 1) { ans += num; } } return ans; };
classSolution: defsumOfUnique(self, nums: List[int]) -> int: ans = 0 state = {} for num in nums: if num notin state: ans += num state[num] = 1 elif state[num] == 1: ans -= num state[num] = 2 return ans
[sol2-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { public: intsumOfUnique(vector<int> &nums){ int ans = 0; unordered_map<int, int> state; for (int num : nums) { if (state[num] == 0) { ans += num; state[num] = 1; } elseif (state[num] == 1) { ans -= num; state[num] = 2; } } return ans; } };
[sol2-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { publicintsumOfUnique(int[] nums) { intans=0; Map<Integer, Integer> state = newHashMap<Integer, Integer>(); for (int num : nums) { if (!state.containsKey(num)) { ans += num; state.put(num, 1); } elseif (state.get(num) == 1) { ans -= num; state.put(num, 2); } } return ans; } }
[sol2-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
publicclassSolution { publicintSumOfUnique(int[] nums) { int ans = 0; Dictionary<int, int> state = new Dictionary<int, int>(); foreach (int num in nums) { if (!state.ContainsKey(num)) { ans += num; state.Add(num, 1); } elseif (state[num] == 1) { ans -= num; state[num] = 2; } } return ans; } }
[sol2-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13
funcsumOfUnique(nums []int) (ans int) { state := map[int]int{} for _, num := range nums { if state[num] == 0 { ans += num state[num] = 1 } elseif state[num] == 1 { ans -= num state[num] = 2 } } return }
typedefstruct { int key; int val; UT_hash_handle hh; } HashEntry;
intsumOfUnique(int* nums, int numsSize){ HashEntry * cnt = NULL; int ans = 0; for (int i = 0; i < numsSize; ++i) { HashEntry * pEntry = NULL; HASH_FIND(hh, cnt, &nums[i], sizeof(int), pEntry); if (NULL == pEntry) { pEntry = (HashEntry *)malloc(sizeof(HashEntry)); pEntry->key = nums[i]; pEntry->val = 1; ans += nums[i]; HASH_ADD(hh, cnt, key, sizeof(int), pEntry); } elseif (pEntry->val == 1){ ans -= nums[i]; pEntry->val = 2; } } HashEntry *curr = NULL, *next = NULL; HASH_ITER(hh, cnt, curr, next) { HASH_DEL(cnt, curr); free(curr); } return ans; }
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
var sumOfUnique = function(nums) { let ans = 0; const state = newMap(); for (const num of nums) { if (!state.has(num)) { ans += num; state.set(num, 1); } elseif (state.get(num) === 1) { ans -= num; state.set(num, 2); } } return ans; };