Sum of Unique Elements
EasyPrompt
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.
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.
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 100
Approaches
3 approaches with complexity analysis and trade-offs.
This approach iterates through each element of the array and, for each element, performs another iteration through the entire array to count its occurrences. If an element's count is exactly one, it's added to the total sum.
Algorithm
- Initialize a variable
sumto 0. - Iterate through the array
numswith an outer loop, fromi = 0ton-1. - For each element
nums[i], initialize acountto 0. - Start an inner loop, from
j = 0ton-1. - Inside the inner loop, if
nums[i] == nums[j], incrementcount. - After the inner loop finishes, check if
count == 1. - If
countis 1, addnums[i]tosum. - After the outer loop finishes, return
sum.
Walkthrough
The brute-force method is the most straightforward way to solve the problem. It involves a nested loop structure. The outer loop picks an element, and the inner loop iterates through the entire array to count how many times that element appears. If the final count for an element is one, it is considered unique and is added to a running total.
Algorithm Steps:
- Initialize a variable
sumto 0. - Loop through the array
numswith an indexi. - For each element
nums[i], initialize a countercountto 0. - Start a nested loop with an index
jto iterate through the array again. - If
nums[i]is the same asnums[j], incrementcount. - After the inner loop completes, if
countis exactly 1, addnums[i]tosum. - Once the outer loop is finished, return the total
sum.
class Solution { public int sumOfUnique(int[] nums) { int sum = 0; for (int i = 0; i < nums.length; i++) { int count = 0; for (int j = 0; j < nums.length; j++) { if (nums[i] == nums[j]) { count++; } } if (count == 1) { sum += nums[i]; } } return sum; }}Complexity
Time
O(n^2), where n is the number of elements in `nums`. For each element, we iterate through the entire array again, leading to a quadratic time complexity.
Space
O(1), as we only use a few extra variables to store the sum and count, regardless of the input size.
Trade-offs
Pros
Simple to conceptualize and implement.
Requires no additional data structures, resulting in constant space complexity.
Cons
Highly inefficient for larger arrays due to the O(n^2) time complexity.
Will likely result in a 'Time Limit Exceeded' error on platforms with larger test cases not constrained like this one.
Solutions
Solution
class Solution {public int sumOfUnique(int[] nums) { int[] cnt = new int[101]; for (int x : nums) { ++cnt[x]; } int ans = 0; for (int x = 0; x < 101; ++x) { if (cnt[x] == 1) { ans += x; } } return ans; }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.