{"id":8062,"date":"2021-02-06T08:20:22","date_gmt":"2021-02-06T16:20:22","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8062"},"modified":"2021-02-06T08:20:55","modified_gmt":"2021-02-06T16:20:55","slug":"leetcode-1748-sum-of-unique-elements","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1748-sum-of-unique-elements\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1748. Sum of Unique Elements"},"content":{"rendered":"\n<p>You are given an integer array&nbsp;<code>nums<\/code>. The unique elements of an array are the elements that appear&nbsp;<strong>exactly once<\/strong>&nbsp;in the array.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>sum<\/strong>&nbsp;of all the unique elements of&nbsp;<\/em><code>nums<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,2]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> The unique elements are [1,3], and the sum is 4.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,1,1,1,1]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There are no unique elements, and the sum is 0.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,4,5]\n<strong>Output:<\/strong> 15\n<strong>Explanation:<\/strong> The unique elements are [1,2,3,4,5], and the sum is 15.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 100<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(100)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int sumOfUnique(vector<int>& nums) {\n    vector<int> seen(101);\n    int ans = 0;\n    for (int x : nums)\n      ++seen[x];\n    for (int x : nums)\n      if (seen[x] == 1) ans += x;\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer array&nbsp;nums. The unique elements of an array are the elements that appear&nbsp;exactly once&nbsp;in the array. Return&nbsp;the&nbsp;sum&nbsp;of all the unique elements&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[20,222,82,240],"class_list":["post-8062","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-array","tag-easy","tag-hashtable","tag-unique","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8062","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8062"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8062\/revisions"}],"predecessor-version":[{"id":8064,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8062\/revisions\/8064"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8062"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}