1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// @Title: 唯一元素的和 (Sum of Unique Elements)
// @Author: 15816537946@163.com
// @Date: 2022-02-06 18:55:27
// @Runtime: 0 ms
// @Memory: 2.1 MB
// struct Solution;
impl Solution {
    pub fn sum_of_unique(nums: Vec<i32>) -> i32 {
        let mut cnt = vec![0; 101];
        for n in nums {
            cnt[n as usize] += 1;
        }

        let mut ans = 0;
        for (i, &n) in cnt.iter().enumerate() {
            if n == 1 {
                ans += i as i32;
            }
        }
        ans
    }
}

#[cfg(test)]
mod tests {

    use super::*;
    #[test]
    fn test_case1() {
        assert_eq!(Solution::sum_of_unique(vec![1, 2, 3, 2]), 4);
    }

    #[test]
    fn test_case2() {
        assert_eq!(Solution::sum_of_unique(vec![1, 1, 1, 1, 1]), 0);
    }
    #[test]
    fn test_case3() {
        assert_eq!(Solution::sum_of_unique(vec![1, 2, 3, 4, 5]), 15);
    }
}