Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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] <= 100Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,2,3,2]
[1,1,1,1,1]
[1,2,3,4,5]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1748: Sum of Unique Elements
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;
}
}
// Accepted solution for LeetCode #1748: Sum of Unique Elements
func sumOfUnique(nums []int) (ans int) {
cnt := [101]int{}
for _, x := range nums {
cnt[x]++
}
for x := 0; x < 101; x++ {
if cnt[x] == 1 {
ans += x
}
}
return
}
# Accepted solution for LeetCode #1748: Sum of Unique Elements
class Solution:
def sumOfUnique(self, nums: List[int]) -> int:
cnt = Counter(nums)
return sum(x for x, v in cnt.items() if v == 1)
// Accepted solution for LeetCode #1748: Sum of Unique Elements
impl Solution {
pub fn sum_of_unique(nums: Vec<i32>) -> i32 {
let mut cnt = [0; 101];
for x in nums {
cnt[x as usize] += 1;
}
let mut ans = 0;
for x in 1..101 {
if cnt[x] == 1 {
ans += x;
}
}
ans as i32
}
}
// Accepted solution for LeetCode #1748: Sum of Unique Elements
function sumOfUnique(nums: number[]): number {
const cnt = new Array(101).fill(0);
for (const x of nums) {
++cnt[x];
}
let ans = 0;
for (let x = 0; x < 101; ++x) {
if (cnt[x] == 1) {
ans += x;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.