Sum of Unique Elements
EASYDescription
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
Checkout 3 different approaches to solve Sum of Unique Elements. Click on different approaches to view the approach and algorithm in detail.
Brute Force with Nested Loops
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.
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 Analysis
Pros and Cons
- Simple to conceptualize and implement.
- Requires no additional data structures, resulting in constant space complexity.
- 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.
Code Solutions
Checking out 3 solutions in different languages for Sum of Unique Elements. Click on different languages to view the code.
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 Solution
Watch the video walkthrough for Sum of Unique Elements
Similar Questions
5 related questions you might find useful
Patterns:
Data Structures:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.