-
[Leetcode] 1748. Sum of Unique ElementsAlgorithm/LeetCode 2021. 8. 22. 17:02
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
### My solution class Solution: def sumOfUnique(self, nums: List[int]) -> int: A1 = dict(zip( nums, [nums.count(i) for i in nums]) ) A2 = [k for k,v in A1.items() if v == 1] return sum(A2) # 앞에서 정리했던 list comprehension to dict 를 활용해서 # unique value count 와 if condition 을 해결해보았다.
'Algorithm > LeetCode' 카테고리의 다른 글
[LeetCode] 1470. Shuffle the Array (0) 2021.09.24 [Leetcode] 1742. Maximum Number of Balls in a Box (0) 2021.09.23 [Leetcode] 961. N-Repeated Element in Size 2N Array (0) 2021.08.22 [LeetCode]#682. Baseball Game (0) 2021.08.13 [Leetcode] 844. Backspace String Compare (0) 2021.08.13