Skip to content

Latest commit

 

History

History
186 lines (152 loc) · 3.66 KB

File metadata and controls

186 lines (152 loc) · 3.66 KB

中文文档

Description

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 <= 100
  • 1 <= nums[i] <= 100

Solutions

Python3

class Solution:
    def sumOfUnique(self, nums: List[int]) -> int:
        counter = Counter(nums)
        return sum(num for num, cnt in counter.items() if cnt == 1)

Java

class Solution {
    public int sumOfUnique(int[] nums) {
        int[] counter = new int[101];
        for (int num : nums) {
            ++counter[num];
        }
        int ans = 0;
        for (int i = 0; i < 101; ++i) {
            if (counter[i] == 1) {
                ans += i;
            }
        }
        return ans;
    }
}

C++

class Solution {
public:
    int sumOfUnique(vector<int>& nums) {
        vector<int> counter(101);
        for (int num : nums) ++counter[num];
        int ans = 0;
        for (int i = 0; i < 101; ++i)
            if (counter[i] == 1)
                ans += i;
        return ans;
    }
};

Go

func sumOfUnique(nums []int) int {
	counter := make([]int, 101)
	for _, num := range nums {
		counter[num]++
	}
	ans := 0
	for i := 0; i < 101; i++ {
		if counter[i] == 1 {
			ans += i
		}
	}
	return ans
}

TypeScript

function sumOfUnique(nums: number[]): number {
    let res = 0;
    const map = new Map();
    for (const num of nums) {
        if (map.has(num)) {
            if (map.get(num)) {
                map.set(num, false);
                res -= num;
            }
        } else {
            map.set(num, true);
            res += num;
        }
    }
    return res;
}

Rust

  • Count
impl Solution {
    pub fn sum_of_unique(nums: Vec<i32>) -> i32 {
        let mut arr = [0; 101];
        for num in nums {
            arr[num as usize] += 1;
        }
        let mut res = 0;
        for i in 1..101 {
            if arr[i] == 1 {
                res += i;
            }
        }
        res as i32
    }
}
  • HashMap
use std::collections::HashMap;

impl Solution {
    pub fn sum_of_unique(nums: Vec<i32>) -> i32 {
        let mut res = 0;
        let mut map = HashMap::new();
        for num in nums {
            if map.contains_key(&num) {
                if *map.get(&num).unwrap() {
                    map.insert(num, false);
                    res -= num;
                }
            } else {
                map.insert(num, true);
                res += num;
            }
        }
        res
    }
}

...