#1748
Easy Algorithms Sum of unique elements
Array Hash Table Counting
79.8% acceptance
Feb 25, 2026
1691
35
Given an integer array nums, return the sum of all unique elements of nums.
The unique elements of an array are the elements that appear exactly once in the array.
Solution
Rust
Time O(n)
Space O(n)
use std::collections::HashMap;
impl Solution {
pub fn sum_of_unique(nums: Vec<i32>) -> i32 {
let mut freq: HashMap<i32, i32> = HashMap::new();
for &n in &nums { *freq.entry(n).or_insert(0) += 1; }
freq.iter().filter(|(_, v)| **v == 1).map(|(&k, _)| k).sum()
}
}