Sum of Unique Elements
Write a function sumOfUnique
that takes an array of integers and returns the sum of all unique elements in the array. A unique element is one that appears exactly once in the array. If there are no unique elements, the function should return 0.
Constraints
- The input array will have at least one element and at most 100 elements.
- Each element in the array is an integer ranging from 1 to 100.
Examples
Input: [4, 3, 2, 4, 1]
Output: 6
Explanation: The unique elements are 3, 2, and 1. Their sum is 3 + 2 + 1 = 6.
Input: [1, 1, 1, 1, 1]
Output: 0
Explanation: There are no unique elements in the array, so the sum is 0.
Tips
- Consider using a data structure like a dictionary (or hash map) to count the occurrences of each element.
- Iterate through the array, updating the count of each element in the dictionary.
- After counting, iterate through the dictionary and sum up the values of the keys that have exactly one occurrence.
Solution
What’s the Exact Problem?
The problem is to calculate the sum of all unique elements in a given array of integers. An element is considered unique if it appears exactly once in the array.
Steps to Understand the Problem
- Understand that we need to identify elements that appear only once in the array.
- Calculate the sum of these unique elements.
- Return 0 if there are no unique elements.
Examples
Input: [4, 3, 2, 4, 1]
Output: 6
Explanation: Unique elements are 3, 2, and 1. Sum = 3 + 2 + 1 = 6.
Input: [1, 1, 1, 1, 1]
Output: 0
Explanation: No unique elements. Sum = 0.
What Your Function Should Do?
The function sumOfUnique should take an array of integers and return the sum of all unique elements in it. If no unique elements are found, it should return 0.
How to Solve the Problem?
- Use a dictionary to count the occurrences of each element in the array.
- Iterate through the array, updating the count for each element in the dictionary.
- After counting, go through the dictionary to sum up the values of elements that have a count of one.
Example Code Solution
def sumOfUnique(nums):
count = {}
for num in nums:
count[num] = count.get(num, 0) + 1
return sum(key for key, value in count.items() if value == 1)
Example Usage
print(sumOfUnique([4, 3, 2, 4, 1])) # Output: 6
print(sumOfUnique([1, 1, 1, 1, 1])) # Output: 0
How this Code Works?
- The
sumOfUnique
function creates a dictionary count to store the frequency of each element. - It iterates over the input array, updating the count of each number.
- Finally, it calculates the sum of keys (elements) in the dictionary where the associated value (count) is 1, meaning the element is unique.
- The sum function is used with a generator expression to efficiently compute the sum of unique elements.
Time Complexity
The time complexity of this solution is O(n), where n is the number of elements in the input array. This is because the solution involves a single pass through the array to count elements and another pass through the dictionary (which has at most n keys) to calculate the sum of unique elements.